page.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 'use client'
  2. import Link from 'next/link'
  3. import { RiArrowLeftLine, RiLockPasswordLine } from '@remixicon/react'
  4. import { useTranslation } from 'react-i18next'
  5. import { useState } from 'react'
  6. import { useRouter, useSearchParams } from 'next/navigation'
  7. import { useContext } from 'use-context-selector'
  8. import { COUNT_DOWN_KEY, COUNT_DOWN_TIME_MS } from '../components/signin/countdown'
  9. import { emailRegex } from '@/config'
  10. import Button from '@/app/components/base/button'
  11. import Input from '@/app/components/base/input'
  12. import Toast from '@/app/components/base/toast'
  13. import { sendResetPasswordCode } from '@/service/common'
  14. import I18NContext from '@/context/i18n'
  15. export default function CheckCode() {
  16. const { t } = useTranslation()
  17. const searchParams = useSearchParams()
  18. const router = useRouter()
  19. const [email, setEmail] = useState('')
  20. const [loading, setIsLoading] = useState(false)
  21. const { locale } = useContext(I18NContext)
  22. const handleGetEMailVerificationCode = async () => {
  23. try {
  24. if (!email) {
  25. Toast.notify({ type: 'error', message: t('login.error.emailEmpty') })
  26. return
  27. }
  28. if (!emailRegex.test(email)) {
  29. Toast.notify({
  30. type: 'error',
  31. message: t('login.error.emailInValid'),
  32. })
  33. return
  34. }
  35. setIsLoading(true)
  36. const res = await sendResetPasswordCode(email, locale)
  37. if (res.result === 'success') {
  38. localStorage.setItem(COUNT_DOWN_KEY, `${COUNT_DOWN_TIME_MS}`)
  39. const params = new URLSearchParams(searchParams)
  40. params.set('token', encodeURIComponent(res.data))
  41. params.set('email', encodeURIComponent(email))
  42. router.push(`/reset-password/check-code?${params.toString()}`)
  43. }
  44. else if (res.code === 'account_not_found') {
  45. Toast.notify({
  46. type: 'error',
  47. message: t('login.error.registrationNotAllowed'),
  48. })
  49. }
  50. else {
  51. Toast.notify({
  52. type: 'error',
  53. message: res.data,
  54. })
  55. }
  56. }
  57. catch (error) {
  58. console.error(error)
  59. }
  60. finally {
  61. setIsLoading(false)
  62. }
  63. }
  64. return <div className='flex flex-col gap-3'>
  65. <div className='bg-background-default-dodge border border-components-panel-border-subtle shadow-lg inline-flex w-14 h-14 justify-center items-center rounded-2xl'>
  66. <RiLockPasswordLine className='w-6 h-6 text-2xl text-text-accent-light-mode-only' />
  67. </div>
  68. <div className='pt-2 pb-4'>
  69. <h2 className='title-4xl-semi-bold text-text-primary'>{t('login.resetPassword')}</h2>
  70. <p className='body-md-regular mt-2 text-text-secondary'>
  71. {t('login.resetPasswordDesc')}
  72. </p>
  73. </div>
  74. <form onSubmit={() => { }}>
  75. <input type='text' className='hidden' />
  76. <div className='mb-2'>
  77. <label htmlFor="email" className='my-2 system-md-semibold text-text-secondary'>{t('login.email')}</label>
  78. <div className='mt-1'>
  79. <Input id='email' type="email" disabled={loading} value={email} placeholder={t('login.emailPlaceholder') as string} onChange={e => setEmail(e.target.value)} />
  80. </div>
  81. <div className='mt-3'>
  82. <Button loading={loading} disabled={loading} variant='primary' className='w-full' onClick={handleGetEMailVerificationCode}>{t('login.sendVerificationCode')}</Button>
  83. </div>
  84. </div>
  85. </form>
  86. <div className='py-2'>
  87. <div className='bg-gradient-to-r from-background-gradient-mask-transparent via-divider-regular to-background-gradient-mask-transparent h-px'></div>
  88. </div>
  89. <Link href={`/signin?${searchParams.toString()}`} className='flex items-center justify-center h-9 text-text-tertiary'>
  90. <div className='inline-block p-1 rounded-full bg-background-default-dimm'>
  91. <RiArrowLeftLine size={12} />
  92. </div>
  93. <span className='ml-2 system-xs-regular'>{t('login.backToLogin')}</span>
  94. </Link>
  95. </div>
  96. }