normalForm.tsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 Toast from '../components/base/toast'
  9. import style from './page.module.css'
  10. // import Tooltip from '@/app/components/base/tooltip/index'
  11. import { IS_CE_EDITION, apiPrefix } from '@/config'
  12. import Button from '@/app/components/base/button'
  13. import { login, oauth } from '@/service/common'
  14. const validEmailReg = /^[\w\.-]+@([\w-]+\.)+[\w-]{2,}$/
  15. type IState = {
  16. formValid: boolean
  17. github: boolean
  18. google: boolean
  19. }
  20. function reducer(state: IState, action: { type: string; payload: any }) {
  21. switch (action.type) {
  22. case 'login':
  23. return {
  24. ...state,
  25. formValid: true,
  26. }
  27. case 'login_failed':
  28. return {
  29. ...state,
  30. formValid: true,
  31. }
  32. case 'github_login':
  33. return {
  34. ...state,
  35. github: true,
  36. }
  37. case 'github_login_failed':
  38. return {
  39. ...state,
  40. github: false,
  41. }
  42. case 'google_login':
  43. return {
  44. ...state,
  45. google: true,
  46. }
  47. case 'google_login_failed':
  48. return {
  49. ...state,
  50. google: false,
  51. }
  52. default:
  53. throw new Error('Unknown action.')
  54. }
  55. }
  56. const NormalForm = () => {
  57. const { t } = useTranslation()
  58. const router = useRouter()
  59. const [state, dispatch] = useReducer(reducer, {
  60. formValid: false,
  61. github: false,
  62. google: false,
  63. })
  64. const [showPassword, setShowPassword] = useState(false)
  65. const [email, setEmail] = useState('')
  66. const [password, setPassword] = useState('')
  67. const [isLoading, setIsLoading] = useState(false)
  68. const handleEmailPasswordLogin = async () => {
  69. if (!validEmailReg.test(email)) {
  70. Toast.notify({
  71. type: 'error',
  72. message: t('login.error.emailInValid'),
  73. })
  74. return
  75. }
  76. try {
  77. setIsLoading(true)
  78. await login({
  79. url: '/login',
  80. body: {
  81. email,
  82. password,
  83. remember_me: true,
  84. },
  85. })
  86. router.push('/apps')
  87. }
  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-[32px] font-bold text-gray-900">{t('login.pageTitle')}</h2>
  124. <p className='mt-1 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 hover:!bg-gray-50 !text-sm !font-medium'
  136. >
  137. <>
  138. <span className={
  139. classNames(
  140. style.githubIcon,
  141. 'w-5 h-5 mr-2',
  142. )
  143. } />
  144. <span className="truncate text-gray-800">{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 hover:!bg-gray-50 !text-sm !font-medium'
  155. >
  156. <>
  157. <span className={
  158. classNames(
  159. style.googleIcon,
  160. 'w-5 h-5 mr-2',
  161. )
  162. } />
  163. <span className="truncate text-gray-800">{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 onSubmit={() => { }}>
  181. <div className='mb-5'>
  182. <label htmlFor="email" className="my-2 block text-sm font-medium text-gray-900">
  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. placeholder={t('login.emailPlaceholder') || ''}
  193. 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'}
  194. />
  195. </div>
  196. </div>
  197. <div className='mb-4'>
  198. <label htmlFor="password" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900">
  199. <span>{t('login.password')}</span>
  200. {/* <Tooltip
  201. selector='forget-password'
  202. htmlContent={
  203. <div>
  204. <div className='font-medium'>{t('login.forget')}</div>
  205. <div className='font-medium text-gray-500'>
  206. <code>
  207. sudo rm -rf /
  208. </code>
  209. </div>
  210. </div>
  211. }
  212. >
  213. <span className='cursor-pointer text-primary-600'>{t('login.forget')}</span>
  214. </Tooltip> */}
  215. </label>
  216. <div className="relative mt-1 rounded-md shadow-sm">
  217. <input
  218. id="password"
  219. value={password}
  220. onChange={e => setPassword(e.target.value)}
  221. type={showPassword ? 'text' : 'password'}
  222. autoComplete="current-password"
  223. placeholder={t('login.passwordPlaceholder') || ''}
  224. 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'}
  225. />
  226. <div className="absolute inset-y-0 right-0 flex items-center pr-3">
  227. <button
  228. type="button"
  229. onClick={() => setShowPassword(!showPassword)}
  230. className="text-gray-400 hover:text-gray-500 focus:outline-none focus:text-gray-500"
  231. >
  232. {showPassword ? '👀' : '😝'}
  233. </button>
  234. </div>
  235. </div>
  236. </div>
  237. <div className='mb-2'>
  238. <Button
  239. type='primary'
  240. onClick={handleEmailPasswordLogin}
  241. disabled={isLoading}
  242. className="w-full !fone-medium !text-sm"
  243. >{t('login.signBtn')}</Button>
  244. </div>
  245. </form>
  246. </>
  247. }
  248. {/* agree to our Terms and Privacy Policy. */}
  249. <div className="w-hull text-center block mt-2 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