'use client' import { useEffect, useState } from 'react' import { useSearchParams } from 'next/navigation' import Image from 'next/image' import PublicShell from '@/components/layout/PublicShell' import { useDashboardI18n } from '@/components/I18nProvider' export default function VerifyEmailPage() { const { language } = useDashboardI18n() const searchParams = useSearchParams() const token = searchParams.get('token') const [status, setStatus] = useState<'loading' | 'success' | 'error'>('loading') const [errorDetail, setErrorDetail] = useState(null) const dict = { en: { verifying: 'Verifying your email…', success: 'Email verified! You can now sign in.', error: 'This verification link is invalid or has expired.', signIn: 'Go to sign in', }, fr: { verifying: 'Vérification de votre email…', success: 'Email vérifié ! Vous pouvez maintenant vous connecter.', error: 'Ce lien de vérification est invalide ou a expiré.', signIn: 'Aller à la connexion', }, ar: { verifying: 'جارٍ التحقق من بريدك الإلكتروني…', success: 'تم التحقق من البريد الإلكتروني! يمكنك الآن تسجيل الدخول.', error: 'رابط التحقق هذا غير صالح أو منتهي الصلاحية.', signIn: 'الذهاب إلى تسجيل الدخول', }, }[language] useEffect(() => { if (!token) { setStatus('error') return } let cancelled = false async function verify() { try { const API_BASE = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:4000/api/v1' const url = `${API_BASE}/auth/employee/verify-email` const res = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ token }), }) if (cancelled) return if (res.ok) { setStatus('success') } else { let detail = `API returned ${res.status}` try { const body = await res.json() if (body?.error) detail += `: ${body.error}` if (body?.message) detail += ` — ${body.message}` } catch { // response wasn't JSON } setErrorDetail(detail) setStatus('error') } } catch (err: any) { if (!cancelled) { setErrorDetail(err?.message ?? 'Network error — could not reach the API') setStatus('error') } } } verify() return () => { cancelled = true } }, [token]) return (
RentalDriveGo
{status === 'loading' && ( <>

{dict.verifying}

)} {status === 'success' && ( <>

{dict.success}

{dict.signIn} )} {status === 'error' && ( <>

{dict.error}

{errorDetail && (

{errorDetail}

)} {dict.signIn} )}
) }