normalForm.tsx 9.7 KB

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