mail-and-password-auth.tsx 5.1 KB

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