ForgotPasswordForm.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. 'use client'
  2. import React, { useEffect, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { useRouter } from 'next/navigation'
  5. import { useForm } from 'react-hook-form'
  6. import { z } from 'zod'
  7. import { zodResolver } from '@hookform/resolvers/zod'
  8. import Loading from '../components/base/loading'
  9. import Input from '../components/base/input'
  10. import Button from '@/app/components/base/button'
  11. import {
  12. fetchInitValidateStatus,
  13. fetchSetupStatus,
  14. sendForgotPasswordEmail,
  15. } from '@/service/common'
  16. import type { InitValidateStatusResponse, SetupStatusResponse } from '@/models/common'
  17. const accountFormSchema = z.object({
  18. email: z
  19. .string()
  20. .min(1, { message: 'login.error.emailInValid' })
  21. .email('login.error.emailInValid'),
  22. })
  23. type AccountFormValues = z.infer<typeof accountFormSchema>
  24. const ForgotPasswordForm = () => {
  25. const { t } = useTranslation()
  26. const router = useRouter()
  27. const [loading, setLoading] = useState(true)
  28. const [isEmailSent, setIsEmailSent] = useState(false)
  29. const { register, trigger, getValues, formState: { errors } } = useForm<AccountFormValues>({
  30. resolver: zodResolver(accountFormSchema),
  31. defaultValues: { email: '' },
  32. })
  33. const handleSendResetPasswordEmail = async (email: string) => {
  34. try {
  35. const res = await sendForgotPasswordEmail({
  36. url: '/forgot-password',
  37. body: { email },
  38. })
  39. if (res.result === 'success')
  40. setIsEmailSent(true)
  41. else console.error('Email verification failed')
  42. }
  43. catch (error) {
  44. console.error('Request failed:', error)
  45. }
  46. }
  47. const handleSendResetPasswordClick = async () => {
  48. if (isEmailSent) {
  49. router.push('/signin')
  50. }
  51. else {
  52. const isValid = await trigger('email')
  53. if (isValid) {
  54. const email = getValues('email')
  55. await handleSendResetPasswordEmail(email)
  56. }
  57. }
  58. }
  59. useEffect(() => {
  60. fetchSetupStatus().then((res: SetupStatusResponse) => {
  61. fetchInitValidateStatus().then((res: InitValidateStatusResponse) => {
  62. if (res.status === 'not_started')
  63. window.location.href = '/init'
  64. })
  65. setLoading(false)
  66. })
  67. }, [])
  68. return (
  69. loading
  70. ? <Loading />
  71. : <>
  72. <div className="sm:mx-auto sm:w-full sm:max-w-md">
  73. <h2 className="text-[32px] font-bold text-gray-900">
  74. {isEmailSent ? t('login.resetLinkSent') : t('login.forgotPassword')}
  75. </h2>
  76. <p className='mt-1 text-sm text-gray-600'>
  77. {isEmailSent ? t('login.checkEmailForResetLink') : t('login.forgotPasswordDesc')}
  78. </p>
  79. </div>
  80. <div className="grow mt-8 sm:mx-auto sm:w-full sm:max-w-md">
  81. <div className="bg-white ">
  82. <form>
  83. {!isEmailSent && (
  84. <div className='mb-5'>
  85. <label htmlFor="email"
  86. className="my-2 flex items-center justify-between text-sm font-medium text-gray-900">
  87. {t('login.email')}
  88. </label>
  89. <div className="mt-1">
  90. <Input
  91. {...register('email')}
  92. placeholder={t('login.emailPlaceholder') || ''}
  93. />
  94. {errors.email && <span className='text-red-400 text-sm'>{t(`${errors.email?.message}`)}</span>}
  95. </div>
  96. </div>
  97. )}
  98. <div>
  99. <Button variant='primary' className='w-full' onClick={handleSendResetPasswordClick}>
  100. {isEmailSent ? t('login.backToSignIn') : t('login.sendResetLink')}
  101. </Button>
  102. </div>
  103. </form>
  104. </div>
  105. </div>
  106. </>
  107. )
  108. }
  109. export default ForgotPasswordForm