mail-and-password-auth.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // import Link from 'next/link'
  2. import { useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { useRouter, useSearchParams } from 'next/navigation'
  5. import { useContext } from 'use-context-selector'
  6. import Button from '@/app/components/base/button'
  7. import Toast from '@/app/components/base/toast'
  8. import { emailRegex } from '@/config'
  9. import { login } from '@/service/common'
  10. import Input from '@/app/components/base/input'
  11. import I18NContext from '@/context/i18n'
  12. import { noop } from 'lodash-es'
  13. import { resolvePostLoginRedirect } from '../utils/post-login-redirect'
  14. import { RiEyeLine, RiEyeOffLine } from '@remixicon/react'
  15. type MailAndPasswordAuthProps = {
  16. isInvite: boolean
  17. isEmailSetup: boolean
  18. allowRegistration: boolean
  19. }
  20. const passwordRegex = /^(?=.*[a-zA-Z])(?=.*\d).{8,}$/
  21. export default function MailAndPasswordAuth({ isInvite, isEmailSetup, allowRegistration }: MailAndPasswordAuthProps) {
  22. const { t } = useTranslation()
  23. const { locale } = useContext(I18NContext)
  24. const router = useRouter()
  25. const searchParams = useSearchParams()
  26. const [showPassword, setShowPassword] = useState(false)
  27. const emailFromLink = decodeURIComponent(searchParams.get('email') || '')
  28. const [email, setEmail] = useState(emailFromLink)
  29. const [password, setPassword] = useState('')
  30. const [isLoading, setIsLoading] = useState(false)
  31. const handleEmailPasswordLogin = async () => {
  32. if (!email) {
  33. Toast.notify({ type: 'error', message: t('login.error.emailEmpty') })
  34. return
  35. }
  36. if (!emailRegex.test(email)) {
  37. Toast.notify({
  38. type: 'error',
  39. message: t('login.error.emailInValid'),
  40. })
  41. return
  42. }
  43. if (!password?.trim()) {
  44. Toast.notify({ type: 'error', message: t('login.error.passwordEmpty') })
  45. return
  46. }
  47. if (!passwordRegex.test(password)) {
  48. Toast.notify({
  49. type: 'error',
  50. message: t('login.error.passwordInvalid'),
  51. })
  52. return
  53. }
  54. try {
  55. setIsLoading(true)
  56. const loginData: Record<string, any> = {
  57. email,
  58. password,
  59. language: locale,
  60. remember_me: true,
  61. }
  62. if (isInvite)
  63. loginData.invite_token = decodeURIComponent(searchParams.get('invite_token') as string)
  64. const res = await login({
  65. url: '/login',
  66. body: loginData,
  67. })
  68. if (res.result === 'success') {
  69. if (isInvite) {
  70. router.replace(`/signin/invite-settings?${searchParams.toString()}`)
  71. }
  72. else {
  73. localStorage.setItem('console_token', res.data.access_token)
  74. localStorage.setItem('refresh_token', res.data.refresh_token)
  75. const redirectUrl = resolvePostLoginRedirect(searchParams)
  76. router.replace(redirectUrl || '/apps')
  77. }
  78. }
  79. else if (res.code === 'account_not_found') {
  80. if (allowRegistration) {
  81. const params = new URLSearchParams()
  82. params.append('email', encodeURIComponent(email))
  83. params.append('token', encodeURIComponent(res.data))
  84. router.replace(`/reset-password/check-code?${params.toString()}`)
  85. }
  86. else {
  87. Toast.notify({
  88. type: 'error',
  89. message: t('login.error.registrationNotAllowed'),
  90. })
  91. }
  92. }
  93. else {
  94. Toast.notify({
  95. type: 'error',
  96. message: res.data,
  97. })
  98. }
  99. }
  100. finally {
  101. setIsLoading(false)
  102. }
  103. }
  104. return <form onSubmit={noop}>
  105. <div className='mb-3'>
  106. <label htmlFor="email" className="system-md-semibold my-2 text-text-secondary">
  107. {t('login.email')}
  108. </label>
  109. <div className="mt-1">
  110. <Input
  111. value={email}
  112. onChange={e => setEmail(e.target.value)}
  113. disabled={isInvite}
  114. id="email"
  115. type="email"
  116. autoComplete="email"
  117. placeholder={t('login.emailPlaceholder') || ''}
  118. tabIndex={1}
  119. />
  120. </div>
  121. </div>
  122. <div className='mb-3'>
  123. <label htmlFor="password" className="my-2 flex items-center justify-between">
  124. <span className='system-md-semibold text-text-secondary'>{t('login.password')}</span>
  125. {/* zlwu12 2025-09-18 注释 */}
  126. {/* <Link
  127. href={`/reset-password?${searchParams.toString()}`}
  128. className={`system-xs-regular ${isEmailSetup ? 'text-components-button-secondary-accent-text' : 'pointer-events-none text-components-button-secondary-accent-text-disabled'}`}
  129. tabIndex={isEmailSetup ? 0 : -1}
  130. aria-disabled={!isEmailSetup}
  131. >
  132. {t('login.forget')}
  133. </Link> */}
  134. </label>
  135. <div className="relative mt-1">
  136. <Input
  137. id="password"
  138. value={password}
  139. onChange={e => setPassword(e.target.value)}
  140. onKeyDown={(e) => {
  141. if (e.key === 'Enter')
  142. handleEmailPasswordLogin()
  143. }}
  144. type={showPassword ? 'text' : 'password'}
  145. autoComplete="current-password"
  146. placeholder={t('login.passwordPlaceholder') || ''}
  147. tabIndex={2}
  148. />
  149. <div className="absolute inset-y-0 right-0 flex items-center">
  150. <Button
  151. type="button"
  152. variant='ghost'
  153. onClick={() => setShowPassword(!showPassword)}
  154. >
  155. {showPassword ? <RiEyeOffLine className="h-4 w-4" /> : <RiEyeLine className="h-4 w-4" />}
  156. </Button>
  157. </div>
  158. </div>
  159. </div>
  160. <div className='mb-2'>
  161. <Button
  162. tabIndex={2}
  163. variant='primary'
  164. onClick={handleEmailPasswordLogin}
  165. disabled={isLoading || !email || !password}
  166. className="w-full"
  167. >{t('login.signBtn')}</Button>
  168. </div>
  169. </form>
  170. }