'use client'; import { useState } from 'react'; import { useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import { z } from 'zod'; import { authService } from '@/lib/services/auth'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Alert, AlertDescription } from '@/components/ui/alert'; import { AlertCircle, Loader2, CheckCircle2 } from 'lucide-react'; const resetRequestSchema = z.object({ email: z.string().email('Invalid email address'), }); const resetConfirmSchema = z .object({ password: z .string() .min(8, 'Password must be at least 8 characters') .regex(/[A-Z]/, 'Password must contain at least one uppercase letter') .regex(/[a-z]/, 'Password must contain at least one lowercase letter') .regex(/[0-9]/, 'Password must contain at least one number'), confirmPassword: z.string(), }) .refine((data) => data.password === data.confirmPassword, { message: "Passwords don't match", path: ['confirmPassword'], }); type ResetRequestData = z.infer; type ResetConfirmData = z.infer; interface PasswordResetFormProps { token?: string; uid?: string; onSuccess?: () => void; onBack?: () => void; } export function PasswordResetForm({ token, uid, onSuccess, onBack }: PasswordResetFormProps) { const [error, setError] = useState(''); const [isLoading, setIsLoading] = useState(false); const [success, setSuccess] = useState(false); const { register: registerRequest, handleSubmit: handleSubmitRequest, formState: { errors: requestErrors }, } = useForm({ resolver: zodResolver(resetRequestSchema), }); const { register: registerConfirm, handleSubmit: handleSubmitConfirm, formState: { errors: confirmErrors }, } = useForm({ resolver: zodResolver(resetConfirmSchema), }); const onSubmitRequest = async (data: ResetRequestData) => { setError(''); setIsLoading(true); try { await authService.requestPasswordReset(data.email); setSuccess(true); } catch (err: any) { setError(err.response?.data?.detail || 'Failed to send reset email'); } finally { setIsLoading(false); } }; const onSubmitConfirm = async (data: ResetConfirmData) => { if (!token || !uid) { setError('Invalid reset link'); return; } setError(''); setIsLoading(true); try { await authService.confirmPasswordReset({ uid, token, new_password: data.password, }); setSuccess(true); setTimeout(() => { onSuccess?.(); }, 2000); } catch (err: any) { setError(err.response?.data?.detail || 'Failed to reset password'); } finally { setIsLoading(false); } }; if (success) { return (

{token ? 'Password Reset!' : 'Email Sent!'}

{token ? 'Your password has been reset successfully. You can now log in with your new password.' : 'Check your email for a link to reset your password. If it doesn\'t appear within a few minutes, check your spam folder.'}

{onBack && ( )}
); } // Reset confirmation form (when token and uid are provided) if (token && uid) { return (

Set New Password

Enter your new password below

{error && ( {error} )}
{confirmErrors.password && (

{confirmErrors.password.message}

)}
{confirmErrors.confirmPassword && (

{confirmErrors.confirmPassword.message}

)}
); } // Reset request form (default) return (

Reset Password

Enter your email and we'll send you a link to reset your password

{error && ( {error} )}
{requestErrors.email && (

{requestErrors.email.message}

)}
{onBack && (
)}
); }