EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
VerifyEmail.jsx
Go to the documentation of this file.
1import { useState } from 'react';
2import { useSearchParams } from 'react-router-dom';
3import PublicLayout from '../components/Layout/PublicLayout';
4import { apiFetch } from '../lib/api';
5
6export default function VerifyEmail() {
7 const [params] = useSearchParams();
8 const token = params.get('token') || '';
9 const [status, setStatus] = useState('idle'); // idle | ok | error
10 const [message, setMessage] = useState('');
11
12 async function handleVerify() {
13 setStatus('loading');
14 try {
15 const res = await apiFetch('/users/verify-email', {
16 method: 'POST',
17 headers: { 'Content-Type': 'application/json' },
18 body: JSON.stringify({ token })
19 });
20 const data = await res.json().catch(()=>({}));
21 if (!res.ok) throw new Error(data.error || 'Verification failed');
22 setStatus('ok');
23 setMessage('Email verified successfully. You can close this page.');
24 } catch (err) {
25 setStatus('error');
26 setMessage(err.message || 'Failed to verify');
27 }
28 }
29
30 return (
31 <PublicLayout>
32 <div style={{ maxWidth: 600, margin: '3rem auto', padding: '2rem', background: '#fff', borderRadius: 12, boxShadow: '0 8px 24px rgba(0,0,0,0.08)' }}>
33 <h1>Verify Email</h1>
34 {status === 'idle' && (
35 <div>
36 <p>Click the button below to verify your email address.</p>
37 <button onClick={handleVerify}>Verify</button>
38 </div>
39 )}
40 {status === 'loading' && <p>Verifying…</p>}
41 {status !== 'idle' && <p>{message}</p>}
42 </div>
43 </PublicLayout>
44 );
45}