normalForm.tsx 10 KB

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