page.tsx 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. 'use client'
  2. import { RiArrowLeftLine, RiMailSendFill } from '@remixicon/react'
  3. import { useTranslation } from 'react-i18next'
  4. import { useState } from 'react'
  5. import { useRouter, useSearchParams } from 'next/navigation'
  6. import { useContext } from 'use-context-selector'
  7. import Countdown from '@/app/components/signin/countdown'
  8. import Button from '@/app/components/base/button'
  9. import Input from '@/app/components/base/input'
  10. import Toast from '@/app/components/base/toast'
  11. import { sendResetPasswordCode, verifyResetPasswordCode } from '@/service/common'
  12. import I18NContext from '@/context/i18n'
  13. export default function CheckCode() {
  14. const { t } = useTranslation()
  15. const router = useRouter()
  16. const searchParams = useSearchParams()
  17. const email = decodeURIComponent(searchParams.get('email') as string)
  18. const token = decodeURIComponent(searchParams.get('token') as string)
  19. const [code, setVerifyCode] = useState('')
  20. const [loading, setIsLoading] = useState(false)
  21. const { locale } = useContext(I18NContext)
  22. const verify = async () => {
  23. try {
  24. if (!code.trim()) {
  25. Toast.notify({
  26. type: 'error',
  27. message: t('login.checkCode.emptyCode'),
  28. })
  29. return
  30. }
  31. if (!/\d{6}/.test(code)) {
  32. Toast.notify({
  33. type: 'error',
  34. message: t('login.checkCode.invalidCode'),
  35. })
  36. return
  37. }
  38. setIsLoading(true)
  39. const ret = await verifyResetPasswordCode({ email, code, token })
  40. ret.is_valid && router.push(`/reset-password/set-password?${searchParams.toString()}`)
  41. }
  42. catch (error) { console.error(error) }
  43. finally {
  44. setIsLoading(false)
  45. }
  46. }
  47. const resendCode = async () => {
  48. try {
  49. const res = await sendResetPasswordCode(email, locale)
  50. if (res.result === 'success') {
  51. const params = new URLSearchParams(searchParams)
  52. params.set('token', encodeURIComponent(res.data))
  53. router.replace(`/reset-password/check-code?${params.toString()}`)
  54. }
  55. }
  56. catch (error) { console.error(error) }
  57. }
  58. return <div className='flex flex-col gap-3'>
  59. <div className='bg-background-default-dodge text-text-accent-light-mode-only border border-components-panel-border-subtle shadow-lg inline-flex w-14 h-14 justify-center items-center rounded-2xl'>
  60. <RiMailSendFill className='w-6 h-6 text-2xl' />
  61. </div>
  62. <div className='pt-2 pb-4'>
  63. <h2 className='title-4xl-semi-bold text-text-primary'>{t('login.checkCode.checkYourEmail')}</h2>
  64. <p className='mt-2 body-md-regular text-text-secondary'>
  65. <span dangerouslySetInnerHTML={{ __html: t('login.checkCode.tips', { email }) as string }}></span>
  66. <br />
  67. {t('login.checkCode.validTime')}
  68. </p>
  69. </div>
  70. <form action="">
  71. <input type='text' className='hidden' />
  72. <label htmlFor="code" className='system-md-semibold text-text-secondary mb-1'>{t('login.checkCode.verificationCode')}</label>
  73. <Input value={code} onChange={e => setVerifyCode(e.target.value)} max-length={6} className='mt-1' placeholder={t('login.checkCode.verificationCodePlaceholder') as string} />
  74. <Button loading={loading} disabled={loading} className='my-3 w-full' variant='primary' onClick={verify}>{t('login.checkCode.verify')}</Button>
  75. <Countdown onResend={resendCode} />
  76. </form>
  77. <div className='py-2'>
  78. <div className='bg-gradient-to-r from-background-gradient-mask-transparent via-divider-regular to-background-gradient-mask-transparent h-px'></div>
  79. </div>
  80. <div onClick={() => router.back()} className='flex items-center justify-center h-9 text-text-tertiary cursor-pointer'>
  81. <div className='inline-block p-1 rounded-full bg-background-default-dimm'>
  82. <RiArrowLeftLine size={12} />
  83. </div>
  84. <span className='ml-2 system-xs-regular'>{t('login.back')}</span>
  85. </div>
  86. </div>
  87. }