You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mail-and-password-auth.tsx 5.1KB

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