|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import React, { useEffect } from 'react'; |
| 4 | +import { useForm } from 'react-hook-form'; |
| 5 | +import { useRouter, useSearchParams } from 'next/navigation'; |
| 6 | +import { toast } from 'sonner'; |
| 7 | + |
| 8 | +import FooterLink from '@/components/forms/FooterLink'; |
| 9 | +import InputField from '@/components/forms/InputField'; |
| 10 | +import OpenDevSocietyBranding from '@/components/OpenDevSocietyBranding'; |
| 11 | +import { Button } from '@/components/ui/button'; |
| 12 | +import { resetPasswordWithToken } from '@/lib/actions/auth.actions'; |
| 13 | + |
| 14 | +type ResetPasswordFormData = { |
| 15 | + newPassword: string; |
| 16 | + confirmPassword: string; |
| 17 | +}; |
| 18 | + |
| 19 | +const ResetPasswordForm = () => { |
| 20 | + const router = useRouter(); |
| 21 | + const searchParams = useSearchParams(); |
| 22 | + const token = searchParams.get('token') ?? ''; |
| 23 | + const error = searchParams.get('error'); |
| 24 | + |
| 25 | + const { |
| 26 | + register, |
| 27 | + watch, |
| 28 | + handleSubmit, |
| 29 | + formState: { errors, isSubmitting }, |
| 30 | + } = useForm<ResetPasswordFormData>({ |
| 31 | + defaultValues: { |
| 32 | + newPassword: '', |
| 33 | + confirmPassword: '', |
| 34 | + }, |
| 35 | + mode: 'onBlur', |
| 36 | + }); |
| 37 | + |
| 38 | + const newPassword = watch('newPassword'); |
| 39 | + |
| 40 | + useEffect(() => { |
| 41 | + if (error === 'INVALID_TOKEN') { |
| 42 | + toast.error('Reset link is invalid or expired.'); |
| 43 | + } |
| 44 | + }, [error]); |
| 45 | + |
| 46 | + const onSubmit = async (data: ResetPasswordFormData) => { |
| 47 | + if (!token) { |
| 48 | + toast.error('Reset link is invalid or expired.'); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + try { |
| 53 | + const result = await resetPasswordWithToken({ |
| 54 | + token, |
| 55 | + newPassword: data.newPassword, |
| 56 | + }); |
| 57 | + |
| 58 | + if (result.success) { |
| 59 | + toast.success('Password updated. You can sign in now.'); |
| 60 | + router.push('/sign-in'); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + toast.error('Password reset failed', { |
| 65 | + description: result.error ?? 'Unable to reset your password.', |
| 66 | + }); |
| 67 | + } catch (error) { |
| 68 | + toast.error('Password reset failed', { |
| 69 | + description: error instanceof Error ? error.message : 'Unable to reset your password.', |
| 70 | + }); |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | + return ( |
| 75 | + <> |
| 76 | + <h1 className="form-title">Choose a new password</h1> |
| 77 | + <p className="text-sm text-gray-400 mb-6"> |
| 78 | + Enter a new password for your account. |
| 79 | + </p> |
| 80 | + |
| 81 | + <form onSubmit={handleSubmit(onSubmit)} className="space-y-5"> |
| 82 | + <InputField |
| 83 | + name="newPassword" |
| 84 | + label="New Password" |
| 85 | + placeholder="Enter a new password" |
| 86 | + type="password" |
| 87 | + register={register} |
| 88 | + error={errors.newPassword} |
| 89 | + validation={{ required: 'New password is required', minLength: 8 }} |
| 90 | + /> |
| 91 | + |
| 92 | + <InputField |
| 93 | + name="confirmPassword" |
| 94 | + label="Confirm Password" |
| 95 | + placeholder="Confirm your new password" |
| 96 | + type="password" |
| 97 | + register={register} |
| 98 | + error={errors.confirmPassword} |
| 99 | + validation={{ |
| 100 | + required: 'Please confirm your new password', |
| 101 | + validate: (value: string) => |
| 102 | + value === newPassword || 'Passwords do not match', |
| 103 | + }} |
| 104 | + /> |
| 105 | + |
| 106 | + <Button type="submit" disabled={isSubmitting} className="yellow-btn w-full mt-5"> |
| 107 | + {isSubmitting ? 'Resetting password' : 'Reset password'} |
| 108 | + </Button> |
| 109 | + |
| 110 | + <FooterLink text="Need a fresh link?" linkText="Request another one" href="/forgot-password" /> |
| 111 | + <OpenDevSocietyBranding outerClassName="mt-10 flex justify-center" /> |
| 112 | + </form> |
| 113 | + </> |
| 114 | + ); |
| 115 | +}; |
| 116 | + |
| 117 | +export default ResetPasswordForm; |
0 commit comments