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