normalForm.tsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. 'use client'
  2. import React, { useEffect, useReducer, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { useRouter } from 'next/navigation'
  5. import classNames from 'classnames'
  6. import useSWR from 'swr'
  7. import Link from 'next/link'
  8. import Toast from '../components/base/toast'
  9. import style from './page.module.css'
  10. import { IS_CE_EDITION, SUPPORT_MAIL_LOGIN, apiPrefix, emailRegex } from '@/config'
  11. import Button from '@/app/components/base/button'
  12. import { login, oauth } from '@/service/common'
  13. import { getPurifyHref } from '@/utils'
  14. type IState = {
  15. formValid: boolean
  16. github: boolean
  17. google: boolean
  18. }
  19. type IAction = {
  20. type: 'login' | 'login_failed' | 'github_login' | 'github_login_failed' | 'google_login' | 'google_login_failed'
  21. }
  22. function reducer(state: IState, action: IAction) {
  23. switch (action.type) {
  24. case 'login':
  25. return {
  26. ...state,
  27. formValid: true,
  28. }
  29. case 'login_failed':
  30. return {
  31. ...state,
  32. formValid: true,
  33. }
  34. case 'github_login':
  35. return {
  36. ...state,
  37. github: true,
  38. }
  39. case 'github_login_failed':
  40. return {
  41. ...state,
  42. github: false,
  43. }
  44. case 'google_login':
  45. return {
  46. ...state,
  47. google: true,
  48. }
  49. case 'google_login_failed':
  50. return {
  51. ...state,
  52. google: false,
  53. }
  54. default:
  55. throw new Error('Unknown action.')
  56. }
  57. }
  58. const NormalForm = () => {
  59. const { t } = useTranslation()
  60. const useEmailLogin = IS_CE_EDITION || SUPPORT_MAIL_LOGIN
  61. const router = useRouter()
  62. const [state, dispatch] = useReducer(reducer, {
  63. formValid: false,
  64. github: false,
  65. google: false,
  66. })
  67. const [showPassword, setShowPassword] = useState(false)
  68. const [email, setEmail] = useState('')
  69. const [password, setPassword] = useState('')
  70. const [isLoading, setIsLoading] = useState(false)
  71. const handleEmailPasswordLogin = async () => {
  72. if (!emailRegex.test(email)) {
  73. Toast.notify({
  74. type: 'error',
  75. message: t('login.error.emailInValid'),
  76. })
  77. return
  78. }
  79. try {
  80. setIsLoading(true)
  81. const res = await login({
  82. url: '/login',
  83. body: {
  84. email,
  85. password,
  86. remember_me: true,
  87. },
  88. })
  89. if (res.result === 'success') {
  90. localStorage.setItem('console_token', res.data)
  91. router.replace('/apps')
  92. }
  93. else {
  94. Toast.notify({
  95. type: 'error',
  96. message: res.data,
  97. })
  98. }
  99. }
  100. finally {
  101. setIsLoading(false)
  102. }
  103. }
  104. const { data: github, error: github_error } = useSWR(state.github
  105. ? ({
  106. url: '/oauth/login/github',
  107. // params: {
  108. // provider: 'github',
  109. // },
  110. })
  111. : null, oauth)
  112. const { data: google, error: google_error } = useSWR(state.google
  113. ? ({
  114. url: '/oauth/login/google',
  115. // params: {
  116. // provider: 'google',
  117. // },
  118. })
  119. : null, oauth)
  120. useEffect(() => {
  121. if (github_error !== undefined)
  122. dispatch({ type: 'github_login_failed' })
  123. if (github)
  124. window.location.href = github.redirect_url
  125. }, [github, github_error])
  126. useEffect(() => {
  127. if (google_error !== undefined)
  128. dispatch({ type: 'google_login_failed' })
  129. if (google)
  130. window.location.href = google.redirect_url
  131. }, [google, google_error])
  132. return (
  133. <>
  134. <div className="w-full mx-auto">
  135. <h2 className="text-[32px] font-bold text-gray-900">{t('login.pageTitle')}</h2>
  136. <p className='mt-1 text-sm text-gray-600'>{t('login.welcome')}</p>
  137. </div>
  138. <div className="w-full mx-auto mt-8">
  139. <div className="bg-white ">
  140. {!useEmailLogin && (
  141. <div className="flex flex-col gap-3 mt-6">
  142. <div className='w-full'>
  143. <a href={getPurifyHref(`${apiPrefix}/oauth/login/github`)}>
  144. <Button
  145. disabled={isLoading}
  146. className='w-full hover:!bg-gray-50'
  147. >
  148. <>
  149. <span className={
  150. classNames(
  151. style.githubIcon,
  152. 'w-5 h-5 mr-2',
  153. )
  154. } />
  155. <span className="truncate text-gray-800">{t('login.withGitHub')}</span>
  156. </>
  157. </Button>
  158. </a>
  159. </div>
  160. <div className='w-full'>
  161. <a href={getPurifyHref(`${apiPrefix}/oauth/login/google`)}>
  162. <Button
  163. disabled={isLoading}
  164. className='w-full hover:!bg-gray-50'
  165. >
  166. <>
  167. <span className={
  168. classNames(
  169. style.googleIcon,
  170. 'w-5 h-5 mr-2',
  171. )
  172. } />
  173. <span className="truncate text-gray-800">{t('login.withGoogle')}</span>
  174. </>
  175. </Button>
  176. </a>
  177. </div>
  178. </div>
  179. )}
  180. {
  181. useEmailLogin && <>
  182. {/* <div className="relative mt-6">
  183. <div className="absolute inset-0 flex items-center" aria-hidden="true">
  184. <div className="w-full border-t border-gray-300" />
  185. </div>
  186. <div className="relative flex justify-center text-sm">
  187. <span className="px-2 text-gray-300 bg-white">OR</span>
  188. </div>
  189. </div> */}
  190. <form onSubmit={() => { }}>
  191. <div className='mb-5'>
  192. <label htmlFor="email" className="my-2 block text-sm font-medium text-gray-900">
  193. {t('login.email')}
  194. </label>
  195. <div className="mt-1">
  196. <input
  197. value={email}
  198. onChange={e => setEmail(e.target.value)}
  199. id="email"
  200. type="email"
  201. autoComplete="email"
  202. placeholder={t('login.emailPlaceholder') || ''}
  203. className={'appearance-none block w-full rounded-lg pl-[14px] px-3 py-2 border border-gray-200 hover:border-gray-300 hover:shadow-sm focus:outline-none focus:ring-primary-500 focus:border-primary-500 placeholder-gray-400 caret-primary-600 sm:text-sm'}
  204. />
  205. </div>
  206. </div>
  207. <div className='mb-4'>
  208. <label htmlFor="password" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900">
  209. <span>{t('login.password')}</span>
  210. <Link href='/forgot-password' className='text-primary-600'>
  211. {t('login.forget')}
  212. </Link>
  213. </label>
  214. <div className="relative mt-1">
  215. <input
  216. id="password"
  217. value={password}
  218. onChange={e => setPassword(e.target.value)}
  219. onKeyDown={(e) => {
  220. if (e.key === 'Enter')
  221. handleEmailPasswordLogin()
  222. }}
  223. type={showPassword ? 'text' : 'password'}
  224. autoComplete="current-password"
  225. placeholder={t('login.passwordPlaceholder') || ''}
  226. className={'appearance-none block w-full rounded-lg pl-[14px] px-3 py-2 border border-gray-200 hover:border-gray-300 hover:shadow-sm focus:outline-none focus:ring-primary-500 focus:border-primary-500 placeholder-gray-400 caret-primary-600 sm:text-sm pr-10'}
  227. />
  228. <div className="absolute inset-y-0 right-0 flex items-center pr-3">
  229. <button
  230. type="button"
  231. onClick={() => setShowPassword(!showPassword)}
  232. className="text-gray-400 hover:text-gray-500 focus:outline-none focus:text-gray-500"
  233. >
  234. {showPassword ? '👀' : '😝'}
  235. </button>
  236. </div>
  237. </div>
  238. </div>
  239. <div className='mb-2'>
  240. <Button
  241. tabIndex={0}
  242. variant='primary'
  243. onClick={handleEmailPasswordLogin}
  244. disabled={isLoading}
  245. className="w-full"
  246. >{t('login.signBtn')}</Button>
  247. </div>
  248. </form>
  249. </>
  250. }
  251. {/* agree to our Terms and Privacy Policy. */}
  252. <div className="w-hull text-center block mt-2 text-xs text-gray-600">
  253. {t('login.tosDesc')}
  254. &nbsp;
  255. <Link
  256. className='text-primary-600'
  257. target='_blank' rel='noopener noreferrer'
  258. href='https://dify.ai/terms'
  259. >{t('login.tos')}</Link>
  260. &nbsp;&&nbsp;
  261. <Link
  262. className='text-primary-600'
  263. target='_blank' rel='noopener noreferrer'
  264. href='https://dify.ai/privacy'
  265. >{t('login.pp')}</Link>
  266. </div>
  267. {IS_CE_EDITION && <div className="w-hull text-center block mt-2 text-xs text-gray-600">
  268. {t('login.goToInit')}
  269. &nbsp;
  270. <Link
  271. className='text-primary-600'
  272. href='/install'
  273. >{t('login.setAdminAccount')}</Link>
  274. </div>}
  275. </div>
  276. </div>
  277. </>
  278. )
  279. }
  280. export default NormalForm