normalForm.tsx 10.0 KB

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