import React, { useState, useEffect } from 'react'; import { Head, Link, router } from '@inertiajs/react'; import axios from 'axios'; import { toast } from 'sonner'; import { ArrowLeft, MailCheck, Loader2, RefreshCw, KeyRound, Users, DollarSign, MessageSquare } from 'lucide-react'; export default function VerifyEmail({ email }) { const [otp, setOtp] = useState(''); const [loading, setLoading] = useState(false); const [resending, setResending] = useState(false); const [errors, setErrors] = useState({}); // Cooldown timer state for resending OTP (60s) const [cooldown, setCooldown] = useState(60); useEffect(() => { let timer; if (cooldown > 0) { timer = setTimeout(() => setCooldown(cooldown - 1), 1000); } return () => clearTimeout(timer); }, [cooldown]); const handleOtpChange = (e) => { const val = e.target.value.replace(/[^0-9]/g, '').slice(0, 6); setOtp(val); if (errors.otp) { setErrors({}); } }; const handleSubmit = (e) => { e.preventDefault(); if (otp.length !== 6) { setErrors({ otp: 'Please enter all 6 digits of the code.' }); return; } router.post('/employer/verify-email', { otp }, { onStart: () => { setLoading(true); setErrors({}); }, onFinish: () => { setLoading(false); }, onSuccess: () => { toast.success('Email verified successfully! Please upload your Emirates ID.'); }, onError: (err) => { // Inertia passes validation errors directly in the err object const otpErr = err.otp || 'Verification failed.'; setErrors({ otp: otpErr }); toast.error(otpErr); } }); }; const handleResend = async () => { if (cooldown > 0 || resending) return; setResending(true); try { await axios.post('/employer/resend-otp'); toast.success('A new verification code has been dispatched to your email.'); setCooldown(60); // Reset timer setOtp(''); } catch (err) { if (err.response) { toast.error(err.response.data?.message || 'Failed to dispatch verification code.'); if (err.response.status === 429) { setCooldown(60); // Reset cooldown on too many requests } } else { toast.error('Network error. Please check your connection.'); } } finally { setResending(false); } }; const renderStepIndicator = () => (
{[ { step: 1, label: 'Register' }, { step: 2, label: 'Verify' }, { step: 3, label: 'Emirates ID' }, { step: 4, label: 'Payment' }, { step: 5, label: 'Password' } ].map((s) => (
{s.step}
{s.step === 2 && ( {s.label} )}
{s.step < 5 && (
)} ))}
); return (
{/* Left Brand Panel (Desktop Only) */}
M
Migrant Portal
Employer Registration

Find verified domestic workers directly.

Unlock direct access to top domestic candidates with a Migrant premium subscription. Find, interview, and hire directly without middlemen.

{/* Stat Row */}
500+
Verified Workers
Premium
Employer Access
Direct
Messaging
© 2026 Migrant. All rights reserved.
{/* Right Verify Form */}
{renderStepIndicator()}

Verify Email

Verify your employer registration

Check Your Email

We sent a 6-digit verification code to: {email}

{errors.otp && (

{Array.isArray(errors.otp) ? errors.otp[0] : errors.otp}

)}
{cooldown > 0 ? (

Resend code in {cooldown}s

) : ( )}
Change Email / Back
); }