normalForm.tsx 9.0 KB

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