'use client'; import { useState } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; import { useRouter } from 'next/navigation'; import Link from 'next/link'; import toast, { Toaster } from 'react-hot-toast'; import { Mail, Key, Lock, CheckCircle, ArrowLeft, RefreshCw } from 'lucide-react'; import { useTranslation } from 'react-i18next'; import { requestForgetPasswordOtp, verifyForgetPasswordOtp } from '../utils/api'; export default function ForgotPasswordPage() { const router = useRouter(); const { t } = useTranslation(); const [step, setStep] = useState(1); const [email, setEmail] = useState(''); const [code, setCode] = useState(''); const [newPassword, setNewPassword] = useState(''); const [isLoading, setIsLoading] = useState(false); const handleRequestOtp = async (e) => { e.preventDefault(); if (!email.trim()) { toast.error(t('forgotPassword.emailRequired')); return; } setIsLoading(true); try { await requestForgetPasswordOtp(email); toast.success(t('forgotPassword.otpSent')); setStep(2); } catch (err) { toast.error(err?.message || t('forgotPassword.otpSendFailed')); } finally { setIsLoading(false); } }; const handleVerifyOtp = async (e) => { e.preventDefault(); if (!code.trim()) { toast.error(t('forgotPassword.otpRequired')); return; } if (newPassword.length < 6) { toast.error(t('validation.passwordMinLength')); return; } setIsLoading(true); try { await verifyForgetPasswordOtp(email, code, newPassword); toast.success(t('forgotPassword.resetSuccess')); setTimeout(() => router.push('/login'), 1200); } catch (err) { toast.error(err?.message || t('forgotPassword.otpVerifyFailed')); } finally { setIsLoading(false); } }; const inputClass = "w-full pr-12 pl-4 py-3 bg-white/10 border border-gray-300 rounded-xl focus:outline-none focus:ring-2 focus:ring-amber-500 focus:border-transparent text-gray-900 placeholder-gray-400 transition-all"; return (
{t('forgotPassword.backToLogin')}
{t('forgotPassword.title')} {step === 1 ? t('forgotPassword.step1Instruction') : t('forgotPassword.step2Instruction')}
{step === 1 && (
setEmail(e.target.value)} className={inputClass} placeholder={t('forgotPassword.emailPlaceholder')} dir="ltr" required />
{isLoading ? (
{t('forgotPassword.sending')}
) : ( t('forgotPassword.sendOtp') )} )} {step === 2 && (
setCode(e.target.value)} className={inputClass} placeholder={t('forgotPassword.otpPlaceholder')} dir="ltr" required />
setNewPassword(e.target.value)} className={inputClass} placeholder={t('forgotPassword.newPasswordPlaceholder')} required minLength={6} />
{isLoading ? (
{t('forgotPassword.verifying')}
) : ( t('forgotPassword.resetPassword') )}
)}
); }