import { useEffect, useState } from 'react'; import { useLocation, useNavigate } from 'react-router-dom'; import api from '../../api'; const VerifyEmailPage = () => { const [status, setStatus] = useState<'verifying' | 'success' | 'error'>('verifying'); const [error, setError] = useState(null); const location = useLocation(); const navigate = useNavigate(); useEffect(() => { const verify = async () => { const params = new URLSearchParams(location.search); const token = params.get('token'); if (!token) { setStatus('error'); setError('No verification token found.'); return; } try { const res = await api.get(`/auth/verify-email?token=${token}`); // Assuming the API returns a token and user object on success localStorage.setItem('token', res.data.token); localStorage.setItem('user', JSON.stringify(res.data.user)); setStatus('success'); // Redirect to home page after a short delay setTimeout(() => { navigate('/'); }, 3000); } catch (err: any) { setStatus('error'); setError(err.response?.data?.error || 'Verification failed. The link may be invalid or expired.'); } }; verify(); }, [location, navigate]); return (
{status === 'verifying' &&

Verifying your email...

} {status === 'success' && (

Email Verified Successfully!

You are now logged in. Redirecting to the homepage...

)} {/* {status === 'error' && (

Verification Failed

{error}

Please try registering again or contact support.

)} */}
); }; export default VerifyEmailPage;