dorfteich/apps/web/src/pages/auth/VerifyEmailPage.tsx
Claude Fable 5 7d10290389
Some checks failed
CD / Build and push images (push) Successful in 39s
CI / Lint, typecheck, test (push) Successful in 1m12s
CI / Auth e2e pack (push) Failing after 1m51s
CI / Build container images (push) Has been skipped
CD / Deploy to Test (push) Successful in 9s
CD / Smoke tests against Test (push) Successful in 1m4s
CD / Promote to Int (push) Successful in 10s
Fire the e-mail verification exactly once per token
React StrictMode double-invokes effects in development; the second
POST consumed-token 400 could win the state race and show an error
for a successful verification (flaked in CI, passed locally). A ref
guards the single-use call; Playwright test-results are ignored.

Part of #20

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-05 05:59:25 +02:00

84 lines
2.4 KiB
TypeScript

import { useEffect, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Link, useSearchParams } from 'react-router-dom';
import { apiPost } from '../../lib/api';
type VerifyState = 'pending' | 'success' | 'error';
export function VerifyEmailPage(): React.JSX.Element {
const { t } = useTranslation();
const [params] = useSearchParams();
const [state, setState] = useState<VerifyState>('pending');
const [email, setEmail] = useState('');
const [resent, setResent] = useState(false);
const token = params.get('token');
// Tokens are single-use: the effect must fire exactly once per token,
// also under React StrictMode's double-invocation in development.
const firedFor = useRef<string | null>(null);
useEffect(() => {
if (!token) {
setState('error');
return;
}
if (firedFor.current === token) return;
firedFor.current = token;
apiPost('/auth/verify-email', { token })
.then(() => setState('success'))
.catch(() => setState('error'));
}, [token]);
if (state === 'pending') {
return (
<div className="auth-card">
<h1>{t('auth:verify.title')}</h1>
</div>
);
}
if (state === 'success') {
return (
<div className="auth-card">
<h1>{t('auth:verify.success.title')}</h1>
<p>{t('auth:verify.success.body')}</p>
<Link className="button" to="/login">
{t('auth:verify.success.login')}
</Link>
</div>
);
}
return (
<div className="auth-card">
<h1>{t('auth:verify.error.title')}</h1>
<p>{t('errors:token_invalid')}</p>
{resent ? (
<p className="form-banner form-banner--ok">{t('auth:verify.resent')}</p>
) : (
<form
onSubmit={(event) => {
event.preventDefault();
void apiPost('/auth/resend-verification', { email }).then(() => setResent(true));
}}
>
<p>{t('auth:verify.error.resendPrompt')}</p>
<label className="field">
<span className="field__label">{t('auth:forgot.email')}</span>
<input
type="email"
value={email}
onChange={(event) => setEmail(event.target.value)}
required
/>
</label>
<button type="submit" className="button">
{t('auth:verify.error.resend')}
</button>
</form>
)}
</div>
);
}