Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | 1x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 1x 1x 11x 11x 3x 11x 8x 2x 2x 11x 8x 8x 11x 8x 11x 11x 1x 10x 10x 2x | /**
* EmailVerification Component
* Story 1.2.4: Email Verification Flow
* Primary flow: user clicks the verification link from their email.
* Fallback: manual 6-digit code entry (collapsed by default).
*/
import React, { useState, useEffect, useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import { useNavigate, useSearchParams } from 'react-router-dom';
import { Box, Typography, Button, Alert, CircularProgress, Collapse } from '@mui/material';
import { BATbernLoader } from '@components/shared/BATbernLoader';
import { CheckCircle, Email as EmailIcon } from '@mui/icons-material';
import { CodeInput } from '../CodeInput/CodeInput';
import { useEmailVerification, useResendVerification } from '@/hooks/useEmailVerification';
export const EmailVerification: React.FC = () => {
const { t } = useTranslation('auth');
const navigate = useNavigate();
const [searchParams] = useSearchParams();
const [code, setCode] = useState('');
const [isVerified, setIsVerified] = useState(false);
const [redirectCountdown, setRedirectCountdown] = useState(3);
const [resendCooldown, setResendCooldown] = useState(0);
const [showManualEntry, setShowManualEntry] = useState(false);
const hasAutoVerifiedRef = React.useRef(false);
const email = searchParams.get('email') || '';
const autoCode = searchParams.get('code');
const {
mutate: verify,
isPending: isLoading,
error,
} = useEmailVerification({
onSuccess: () => {
setIsVerified(true);
sessionStorage.setItem('userRole', 'ATTENDEE');
},
});
const { mutate: resend } = useResendVerification();
const handleVerification = useCallback(
(verificationCode: string) => {
verify({ email, code: verificationCode });
},
[email, verify]
);
// Auto-verification from email link (prevent double execution)
useEffect(() => {
if (autoCode && email && !hasAutoVerifiedRef.current) {
hasAutoVerifiedRef.current = true;
handleVerification(autoCode);
}
}, [autoCode, email, handleVerification]);
// Redirect countdown - redirect to login after verification
useEffect(() => {
Iif (isVerified && redirectCountdown > 0) {
const timer = setTimeout(() => setRedirectCountdown(redirectCountdown - 1), 1000);
return () => clearTimeout(timer);
} else if (IisVerified && redirectCountdown === 0) {
// Redirect to login page after email verification
navigate('/auth/login');
}
}, [isVerified, redirectCountdown, navigate]);
// Resend cooldown
useEffect(() => {
Iif (resendCooldown > 0) {
const timer = setTimeout(() => setResendCooldown(resendCooldown - 1), 1000);
return () => clearTimeout(timer);
}
}, [resendCooldown]);
const handleResend = () => {
resend(email, {
onSuccess: () => {
setResendCooldown(60);
},
});
};
if (autoCode && isLoading) {
return (
<Box sx={{ maxWidth: 400, mx: 'auto', mt: 8, textAlign: 'center' }}>
<BATbernLoader size={128} />
<Typography variant="h5" sx={{ mt: 2 }}>
{t('verify.verifying')}
</Typography>
</Box>
);
}
Iif (isVerified) {
return (
<Box sx={{ maxWidth: 400, mx: 'auto', mt: 8, textAlign: 'center' }}>
<CheckCircle sx={{ fontSize: 64, color: 'success.main', mb: 2 }} />
<Typography variant="h4" gutterBottom>
{t('verify.successTitle')}
</Typography>
<Typography variant="body1" sx={{ mb: 3 }}>
{t('verify.successMessage')}
</Typography>
<Button
variant="contained"
fullWidth
onClick={() => navigate('/auth/login')}
sx={{ mb: 2 }}
>
{t('verify.goToLogin')}
</Button>
<Typography variant="body2" color="text.secondary">
{t('verify.redirecting', { seconds: redirectCountdown })}
</Typography>
</Box>
);
}
return (
<Box sx={{ maxWidth: 500, mx: 'auto', mt: 8 }}>
{/* Primary: check-your-email messaging */}
<Box sx={{ textAlign: 'center', mb: 4 }}>
<EmailIcon sx={{ fontSize: 64, color: 'primary.main', mb: 2 }} />
<Typography variant="h4" gutterBottom>
{t('verify.title')}
</Typography>
<Typography variant="body1" color="text.secondary" sx={{ mb: 1 }}>
{t('verify.subtitle')}
</Typography>
<Typography variant="h6" sx={{ mb: 2 }}>
{email}
</Typography>
<Typography variant="body1">{t('verify.instruction')}</Typography>
</Box>
{error && (
<Alert severity="error" sx={{ mb: 2 }}>
{t(`verify.errors.${error.message}`)}
</Alert>
)}
{/* Resend */}
<Box sx={{ textAlign: 'center', mb: 3 }}>
<Button onClick={handleResend} disabled={resendCooldown > 0} variant="outlined" fullWidth>
{resendCooldown > 0
? t('verify.resendCooldown', { seconds: resendCooldown })
: t('verify.resendLink')}
</Button>
</Box>
{/* Fallback: manual code entry */}
<Box sx={{ textAlign: 'center' }}>
<Button
size="small"
onClick={() => setShowManualEntry((v) => !v)}
sx={{ color: 'text.secondary' }}
>
{showManualEntry ? t('verify.manualHide') : t('verify.manualToggle')}
</Button>
</Box>
<Collapse in={showManualEntry} unmountOnExit>
<Box sx={{ mt: 2 }}>
<Typography variant="body2" align="center" sx={{ mb: 2 }}>
{t('verify.codeLabel')}
</Typography>
<CodeInput
length={6}
onComplete={handleVerification}
onCodeChange={setCode}
error={!!error}
/>
<Button
variant="contained"
fullWidth
size="large"
onClick={() => handleVerification(code)}
disabled={code.length !== 6 || isLoading}
sx={{ mt: 3 }}
>
{isLoading ? <CircularProgress size={24} /> : t('verify.submitButton')}
</Button>
</Box>
</Collapse>
<Alert severity="info" sx={{ mt: 4 }}>
<Typography variant="body2">{t('verify.expirationNotice')}</Typography>
<Typography variant="body2" sx={{ mt: 1 }}>
{t('verify.checkSpam')}
</Typography>
</Alert>
</Box>
);
};
export default EmailVerification;
|