normalForm.tsx 10 KB

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