'use client'; import { useState, useEffect, FormEvent, type ChangeEvent } from 'react'; import { useRouter } from 'next/navigation'; import { useAuth } from '@/lib/auth/AuthContext'; import { useForgotPassword } from "@/lib/hooks/auth/useForgotPassword"; import Link from 'next/link'; import { motion } from 'framer-motion'; import { Mail, ArrowLeft, AlertCircle, Flag, Shield, CheckCircle2, } from 'lucide-react'; import Card from '@/components/ui/Card'; import Button from '@/components/ui/Button'; import Input from '@/components/ui/Input'; import Heading from '@/components/ui/Heading'; interface FormErrors { email?: string; submit?: string; } interface SuccessState { message: string; magicLink?: string; } export default function ForgotPasswordPage() { const router = useRouter(); const { session } = useAuth(); const [errors, setErrors] = useState({}); const [success, setSuccess] = useState(null); const [formData, setFormData] = useState({ email: '', }); // Check if user is already authenticated useEffect(() => { if (session) { router.replace('/dashboard'); } }, [session, router]); const validateForm = (): boolean => { const newErrors: FormErrors = {}; if (!formData.email.trim()) { newErrors.email = 'Email is required'; } else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(formData.email)) { newErrors.email = 'Invalid email format'; } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; // Use forgot password mutation hook const forgotPasswordMutation = useForgotPassword({ onSuccess: (result) => { setSuccess({ message: result.message, magicLink: result.magicLink, }); }, onError: (error) => { setErrors({ submit: error.message || 'Failed to send reset link. Please try again.', }); }, }); const handleSubmit = async (e: FormEvent) => { e.preventDefault(); if (forgotPasswordMutation.isPending) return; if (!validateForm()) return; setErrors({}); setSuccess(null); try { await forgotPasswordMutation.mutateAsync({ email: formData.email }); } catch (error) { // Error handling is done in the mutation's onError callback } }; // Loading state from mutation const loading = forgotPasswordMutation.isPending; return (
{/* Background Pattern */}
{/* Header */}
Reset Password

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

{/* Background accent */}
{!success ? (
{/* Email */}
) => setFormData({ ...formData, email: e.target.value })} error={!!errors.email} errorMessage={errors.email} placeholder="you@example.com" disabled={loading} className="pl-10" autoComplete="email" />
{/* Error Message */} {errors.submit && (

{errors.submit}

)} {/* Submit Button */} {/* Back to Login */}
Back to Login
) : (

{success.message}

{success.magicLink && (

Development Mode - Magic Link:

{success.magicLink}

In production, this would be sent via email

)}
)} {/* Trust Indicators */}
Secure reset process
15 minute expiration
{/* Footer */}

Need help?{' '} Contact support

); }