'use client'; import React, { useEffect, useState } from 'react'; import { useSearchParams, useRouter } from 'next/navigation'; import Navbar from '@/app/components/global/navbar'; const VerifyEmail = () => { const searchParams = useSearchParams(); const router = useRouter(); const [status, setStatus] = useState<'verifying' | 'success' | 'error'>('verifying'); const [message, setMessage] = useState(''); useEffect(() => { const verifyEmail = async () => { const token = searchParams.get('token'); if (!token) { setStatus('error'); setMessage('Verification token is missing'); return; } try { const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8080'; const response = await fetch(`${API_URL}/auth/verify-email?token=${token}`); if (response.ok) { setStatus('success'); setMessage('Email verified successfully!'); setTimeout(() => router.push('/login'), 3000); } else { const data = await response.json(); setStatus('error'); setMessage(data.message || 'Verification failed'); } } catch (error) { setStatus('error'); setMessage('An error occurred during verification'); } }; verifyEmail(); }, [searchParams, router]); return ( <>
{status === 'verifying' && ( <>
Loading...

Verifying your email...

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

Email Verified!

{message}

Redirecting to login page...

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

Verification Failed

{message}

)}
); }; export default VerifyEmail;