"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"; import InteractiveBackground from "../components/Animation/Background"; 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); } }; 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 (
{user?.email || t("accountVerification.emailNotRegistered")}
{emailVerified ? t("accountVerification.emailVerifiedDesc") : t("accountVerification.emailVerifyDesc")}
{t("accountVerification.enter6DigitCode")}
{user?.phone || t("accountVerification.phoneNotRegistered")}
{phoneVerified ? t("accountVerification.phoneVerifiedDesc") : t("accountVerification.phoneVerifyDesc")}
{t("accountVerification.enter6DigitCodePhone")}