'use client'; import React, { useEffect } from 'react'; import { useForm } from 'react-hook-form'; import { useRouter, useSearchParams } from 'next/navigation'; import { toast } from 'sonner'; import FooterLink from '@/components/forms/FooterLink'; import InputField from '@/components/forms/InputField'; import PasswordRequirements from '@/components/forms/PasswordRequirements'; import OpenDevSocietyBranding from '@/components/OpenDevSocietyBranding'; import { Button } from '@/components/ui/button'; import { resetPasswordWithToken } from '@/lib/actions/auth.actions'; import { PASSWORD_VALIDATION } from '@/lib/constants'; type ResetPasswordFormData = { newPassword: string; confirmPassword: string; }; const ResetPasswordForm = () => { const router = useRouter(); const searchParams = useSearchParams(); const token = searchParams.get('token') ?? ''; const error = searchParams.get('error'); const { register, watch, handleSubmit, formState: { errors, isSubmitting }, } = useForm({ defaultValues: { newPassword: '', confirmPassword: '', }, mode: 'onBlur', }); const newPassword = watch('newPassword'); useEffect(() => { if (error === 'INVALID_TOKEN') { toast.error('Reset link is invalid or expired.'); } }, [error]); const onSubmit = async (data: ResetPasswordFormData) => { if (!token) { toast.error('Reset link is invalid or expired.'); return; } try { const result = await resetPasswordWithToken({ token, newPassword: data.newPassword, }); if (result.success) { toast.success('Password updated. You can sign in now.'); router.push('/sign-in'); return; } toast.error('Password reset failed', { description: result.error ?? 'Unable to reset your password.', }); } catch (error) { toast.error('Password reset failed', { description: error instanceof Error ? error.message : 'Unable to reset your password.', }); } }; return ( <>

Choose a new password

Enter a new password for your account.

value === newPassword || 'Passwords do not match', }} /> ); }; export default ResetPasswordForm;