normalForm.tsx 10 KB

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