'use client'; import { useState, useEffect, FormEvent, type ChangeEvent } from 'react'; import { useRouter } from 'next/navigation'; import { useAuth } from '@/lib/auth/AuthContext'; 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 [loading, setLoading] = useState(false); 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; }; const handleSubmit = async (e: FormEvent) => { e.preventDefault(); if (loading) return; if (!validateForm()) return; setLoading(true); setErrors({}); setSuccess(null); try { const { ServiceFactory } = await import('@/lib/services/ServiceFactory'); const serviceFactory = new ServiceFactory(process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:3001'); const authService = serviceFactory.createAuthService(); const result = await authService.forgotPassword({ email: formData.email }); setSuccess({ message: result.message, magicLink: result.magicLink, }); } catch (error) { setErrors({ submit: error instanceof Error ? error.message : 'Failed to send reset link. Please try again.', }); setLoading(false); } }; 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

); }