'use client'; import { useState, useEffect } from 'react'; import { useRouter } from 'next/navigation'; import Link from 'next/link'; import { motion } from 'framer-motion'; import { useTranslation } from 'react-i18next'; import { Mail, Phone, Shield, ChevronLeft, Loader2, CheckCircle, XCircle, Send, Key } from 'lucide-react'; import toast, { Toaster } from 'react-hot-toast'; import AuthService from '../services/AuthService'; import { sendEmailOTP, sendPhoneOTP, verifyEmail, verifyPhone } from '../utils/api'; export default function AccountVerificationPage() { const router = useRouter(); const { t } = useTranslation(); const [user, setUser] = useState(null); const [isLoading, setIsLoading] = useState(true); const [emailCode, setEmailCode] = useState(''); const [phoneCode, setPhoneCode] = useState(''); const [sendingEmailOTP, setSendingEmailOTP] = useState(false); const [sendingPhoneOTP, setSendingPhoneOTP] = useState(false); const [verifyingEmail, setVerifyingEmail] = useState(false); const [verifyingPhone, setVerifyingPhone] = useState(false); const [emailVerified, setEmailVerified] = useState(false); const [phoneVerified, setPhoneVerified] = useState(false); const [showEmailInput, setShowEmailInput] = useState(false); const [showPhoneInput, setShowPhoneInput] = useState(false); useEffect(() => { const authUser = AuthService.getUser(); if (authUser) { setUser(authUser); setIsLoading(false); } else { router.push('/login'); } }, [router]); const handleSendEmailOTP = async () => { setSendingEmailOTP(true); try { await sendEmailOTP(); toast.success(t('accountVerification.emailOtpSent')); setShowEmailInput(true); } catch (err) { toast.error(err.message || t('accountVerification.otpSendFailed')); } finally { setSendingEmailOTP(false); } }; const handleVerifyEmail = async () => { if (!emailCode.trim()) { toast.error(t('accountVerification.otpRequired')); return; } setVerifyingEmail(true); try { await verifyEmail(emailCode.trim()); setEmailVerified(true); setShowEmailInput(false); toast.success(t('accountVerification.emailVerifiedSuccess')); } catch (err) { toast.error(err.message || t('accountVerification.otpInvalid')); } finally { setVerifyingEmail(false); } }; const handleSendPhoneOTP = async () => { setSendingPhoneOTP(true); try { await sendPhoneOTP(); toast.success(t('accountVerification.phoneOtpSent')); setShowPhoneInput(true); } catch (err) { toast.error(err.message || t('accountVerification.otpSendFailed')); } finally { setSendingPhoneOTP(false); } }; const handleVerifyPhone = async () => { if (!phoneCode.trim()) { toast.error(t('accountVerification.otpRequired')); return; } setVerifyingPhone(true); try { await verifyPhone(phoneCode.trim()); setPhoneVerified(true); setShowPhoneInput(false); toast.success(t('accountVerification.phoneVerifiedSuccess')); } catch (err) { toast.error(err.message || t('accountVerification.otpInvalid')); } finally { setVerifyingPhone(false); } }; if (isLoading) { return (
); } const containerVariants = { hidden: { opacity: 0 }, visible: { opacity: 1, transition: { staggerChildren: 0.1 } } }; const itemVariants = { hidden: { opacity: 0, y: 20 }, visible: { opacity: 1, y: 0 } }; return (

{t('accountVerification.title')}

{t('accountVerification.emailSection')}

{emailVerified && ( {t('accountVerification.verified')} )}

{user?.email || t('accountVerification.emailNotRegistered')}

{emailVerified ? t('accountVerification.emailVerifiedDesc') : t('accountVerification.emailVerifyDesc')}

{!emailVerified && !showEmailInput && ( )}
{showEmailInput && (

{t('accountVerification.enter6DigitCode')}

setEmailCode(e.target.value)} placeholder={t('accountVerification.otpPlaceholder')} maxLength={6} className="flex-1 px-4 py-3 border border-gray-300 rounded-xl focus:ring-2 focus:ring-amber-500 focus:border-transparent outline-none text-center text-lg tracking-widest" />
)} {emailVerified && (
{t('accountVerification.emailVerifiedSuccess')}
)}

{t('accountVerification.phoneSection')}

{phoneVerified && ( {t('accountVerification.verified')} )}

{user?.phone || t('accountVerification.phoneNotRegistered')}

{phoneVerified ? t('accountVerification.phoneVerifiedDesc') : t('accountVerification.phoneVerifyDesc')}

{!phoneVerified && !showPhoneInput && ( )}
{showPhoneInput && (

{t('accountVerification.enter6DigitCodePhone')}

setPhoneCode(e.target.value)} placeholder={t('accountVerification.otpPlaceholder')} maxLength={6} className="flex-1 px-4 py-3 border border-gray-300 rounded-xl focus:ring-2 focus:ring-amber-500 focus:border-transparent outline-none text-center text-lg tracking-widest" />
)} {phoneVerified && (
{t('accountVerification.phoneVerifiedSuccess')}
)}

{t('accountVerification.whyVerify')}

  • {t('accountVerification.reason1')}
  • {t('accountVerification.reason2')}
  • {t('accountVerification.reason3')}
); }