'use client'; import { useState, useEffect } from 'react'; import { useRouter } from 'next/navigation'; import Link from 'next/link'; import { motion } from 'framer-motion'; 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 [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('تم إرسال رمز التحقق إلى بريدك الإلكتروني'); setShowEmailInput(true); } catch (err) { toast.error(err.message || 'فشل إرسال رمز التحقق'); } finally { setSendingEmailOTP(false); } }; const handleVerifyEmail = async () => { if (!emailCode.trim()) { toast.error('الرجاء إدخال رمز التحقق'); return; } setVerifyingEmail(true); try { await verifyEmail(emailCode.trim()); setEmailVerified(true); setShowEmailInput(false); toast.success('تم التحقق من البريد الإلكتروني بنجاح'); } catch (err) { toast.error(err.message || 'رمز التحقق غير صحيح'); } finally { setVerifyingEmail(false); } }; const handleSendPhoneOTP = async () => { setSendingPhoneOTP(true); try { await sendPhoneOTP(); toast.success('تم إرسال رمز التحقق إلى هاتفك'); setShowPhoneInput(true); } catch (err) { toast.error(err.message || 'فشل إرسال رمز التحقق'); } finally { setSendingPhoneOTP(false); } }; const handleVerifyPhone = async () => { if (!phoneCode.trim()) { toast.error('الرجاء إدخال رمز التحقق'); return; } setVerifyingPhone(true); try { await verifyPhone(phoneCode.trim()); setPhoneVerified(true); setShowPhoneInput(false); toast.success('تم التحقق من رقم الهاتف بنجاح'); } catch (err) { toast.error(err.message || 'رمز التحقق غير صحيح'); } finally { setVerifyingPhone(false); } }; if (isLoading) { return (
{user?.email || 'بريد إلكتروني غير مسجل'}
{emailVerified ? 'بريدك الإلكتروني موثق ويمكنك استخدامه لتسجيل الدخول' : 'يرجى توثيق بريدك الإلكتروني لحماية حسابك'}
أدخل الرمز المكون من 6 أرقام الذي تم إرساله إلى بريدك الإلكتروني
{user?.phone || 'رقم هاتف غير مسجل'}
{phoneVerified ? 'رقم هاتفك موثق ويمكنك استخدامه لتسجيل الدخول' : 'يرجى توثيق رقم هاتفك لحماية حسابك'}
أدخل الرمز المكون من 6 أرقام الذي تم إرساله إلى هاتفك