normalForm.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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, 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 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 (!validEmailReg.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. {!IS_CE_EDITION && (
  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. type='default'
  146. disabled={isLoading}
  147. className='w-full hover:!bg-gray-50 !text-sm !font-medium'
  148. >
  149. <>
  150. <span className={
  151. classNames(
  152. style.githubIcon,
  153. 'w-5 h-5 mr-2',
  154. )
  155. } />
  156. <span className="truncate text-gray-800">{t('login.withGitHub')}</span>
  157. </>
  158. </Button>
  159. </a>
  160. </div>
  161. <div className='w-full'>
  162. <a href={getPurifyHref(`${apiPrefix}/oauth/login/google`)}>
  163. <Button
  164. type='default'
  165. disabled={isLoading}
  166. className='w-full hover:!bg-gray-50 !text-sm !font-medium'
  167. >
  168. <>
  169. <span className={
  170. classNames(
  171. style.googleIcon,
  172. 'w-5 h-5 mr-2',
  173. )
  174. } />
  175. <span className="truncate text-gray-800">{t('login.withGoogle')}</span>
  176. </>
  177. </Button>
  178. </a>
  179. </div>
  180. </div>
  181. )}
  182. {
  183. IS_CE_EDITION && <>
  184. {/* <div className="relative mt-6">
  185. <div className="absolute inset-0 flex items-center" aria-hidden="true">
  186. <div className="w-full border-t border-gray-300" />
  187. </div>
  188. <div className="relative flex justify-center text-sm">
  189. <span className="px-2 text-gray-300 bg-white">OR</span>
  190. </div>
  191. </div> */}
  192. <form onSubmit={() => { }}>
  193. <div className='mb-5'>
  194. <label htmlFor="email" className="my-2 block text-sm font-medium text-gray-900">
  195. {t('login.email')}
  196. </label>
  197. <div className="mt-1">
  198. <input
  199. value={email}
  200. onChange={e => setEmail(e.target.value)}
  201. id="email"
  202. type="email"
  203. autoComplete="email"
  204. placeholder={t('login.emailPlaceholder') || ''}
  205. 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'}
  206. />
  207. </div>
  208. </div>
  209. <div className='mb-4'>
  210. <label htmlFor="password" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900">
  211. <span>{t('login.password')}</span>
  212. {/* <Tooltip
  213. selector='forget-password'
  214. htmlContent={
  215. <div>
  216. <div className='font-medium'>{t('login.forget')}</div>
  217. <div className='font-medium text-gray-500'>
  218. <code>
  219. sudo rm -rf /
  220. </code>
  221. </div>
  222. </div>
  223. }
  224. >
  225. <span className='cursor-pointer text-primary-600'>{t('login.forget')}</span>
  226. </Tooltip> */}
  227. </label>
  228. <div className="relative mt-1">
  229. <input
  230. id="password"
  231. value={password}
  232. onChange={e => setPassword(e.target.value)}
  233. onKeyDown={(e) => {
  234. if (e.key === 'Enter')
  235. handleEmailPasswordLogin()
  236. }}
  237. type={showPassword ? 'text' : 'password'}
  238. autoComplete="current-password"
  239. placeholder={t('login.passwordPlaceholder') || ''}
  240. 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'}
  241. />
  242. <div className="absolute inset-y-0 right-0 flex items-center pr-3">
  243. <button
  244. type="button"
  245. onClick={() => setShowPassword(!showPassword)}
  246. className="text-gray-400 hover:text-gray-500 focus:outline-none focus:text-gray-500"
  247. >
  248. {showPassword ? '👀' : '😝'}
  249. </button>
  250. </div>
  251. </div>
  252. </div>
  253. <div className='mb-2'>
  254. <Button
  255. tabIndex={0}
  256. type='primary'
  257. onClick={handleEmailPasswordLogin}
  258. disabled={isLoading}
  259. className="w-full !fone-medium !text-sm"
  260. >{t('login.signBtn')}</Button>
  261. </div>
  262. </form>
  263. </>
  264. }
  265. {/* agree to our Terms and Privacy Policy. */}
  266. <div className="w-hull text-center block mt-2 text-xs text-gray-600">
  267. {t('login.tosDesc')}
  268. &nbsp;
  269. <Link
  270. className='text-primary-600'
  271. target='_blank' rel='noopener noreferrer'
  272. href='https://dify.ai/terms'
  273. >{t('login.tos')}</Link>
  274. &nbsp;&&nbsp;
  275. <Link
  276. className='text-primary-600'
  277. target='_blank' rel='noopener noreferrer'
  278. href='https://dify.ai/privacy'
  279. >{t('login.pp')}</Link>
  280. </div>
  281. {IS_CE_EDITION && <div className="w-hull text-center block mt-2 text-xs text-gray-600">
  282. {t('login.goToInit')}
  283. &nbsp;
  284. <Link
  285. className='text-primary-600'
  286. href='/install'
  287. >{t('login.setAdminAccount')}</Link>
  288. </div>}
  289. </div>
  290. </div>
  291. </>
  292. )
  293. }
  294. export default NormalForm