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