Events 2.3.0 (phase 1c): two-factor with an authenticator app, recovery codes

- TOTP (RFC 6238) next to email and WhatsApp: QR code and key to set up,
  the app's first code confirms it, a code can't be used twice, nothing
  is sent. The secret is stored encrypted.
- 10 single-use recovery codes whenever two-factor is turned on, shown
  once; accepted at sign-in in place of any code; a new set on request.
- Admins see each account's method; Reset 2FA clears the app and codes.
- Profile & Security, sign-in and Users pages; API types; 10 new tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 14:55:22 +02:00
co-authored by Claude Opus 5.5
parent b207d17db8
commit ee65ff358c
18 changed files with 815 additions and 60 deletions
@@ -27,6 +27,8 @@ export function LoginForm() {
// Two-factor step — set once the password is accepted for an account with 2FA on.
const [challenge, setChallenge] = useState<TwoFactorChallenge | null>(null);
const [code, setCode] = useState("");
// 2.3.0: a recovery code instead of the 6-digit code (xxxxx-xxxxx, single use).
const [useRecovery, setUseRecovery] = useState(false);
const [rememberDevice, setRememberDevice] = useState(false);
const [resendIn, setResendIn] = useState(0);
@@ -109,6 +111,7 @@ export function LoginForm() {
const backToPassword = () => {
setChallenge(null);
setCode("");
setUseRecovery(false);
setPassword("");
setError(null);
};
@@ -124,20 +127,24 @@ export function LoginForm() {
</p>
)}
<p className="text-sm text-gray-600 mt-1">
We sent a 6-digit code to {challenge.destination} by {challenge.method === "whatsapp" ? "WhatsApp" : "email"}. It expires in 10 minutes.
{useRecovery
? "Enter one of the recovery codes you saved when you turned on two-factor. Each works once."
: challenge.method === "authenticator"
? "Open your authenticator app and enter the 6-digit code it shows for this site."
: <>We sent a 6-digit code to {challenge.destination} by {challenge.method === "whatsapp" ? "WhatsApp" : "email"}. It expires in 10 minutes.</>}
</p>
</div>
<div>
<label htmlFor="login-code" className="block text-sm font-medium mb-1">Verification code</label>
<label htmlFor="login-code" className="block text-sm font-medium mb-1">{useRecovery ? "Recovery code" : "Verification code"}</label>
<input
id="login-code"
type="text"
inputMode="numeric"
autoComplete="one-time-code"
maxLength={6}
inputMode={useRecovery ? "text" : "numeric"}
autoComplete={useRecovery ? "off" : "one-time-code"}
maxLength={useRecovery ? 11 : 6}
value={code}
onChange={(e) => setCode(e.target.value.replace(/\D/g, ""))}
onChange={(e) => setCode(useRecovery ? e.target.value.toLowerCase().replace(/[^a-z0-9-]/g, "") : e.target.value.replace(/\D/g, ""))}
className="w-full border rounded px-3 py-2 text-lg tracking-widest"
autoFocus
required
@@ -158,7 +165,7 @@ export function LoginForm() {
<button
type="submit"
disabled={loading || code.length !== 6}
disabled={loading || (useRecovery ? code.replace(/-/g, "").length !== 10 : code.length !== 6)}
className="w-full bg-brand-600 hover:bg-brand-700 text-white rounded py-2 disabled:opacity-60"
>
{loading ? "Verifying..." : "Verify and log in"}
@@ -168,14 +175,21 @@ export function LoginForm() {
<button type="button" onClick={backToPassword} className="text-brand-600 hover:underline">
Back to login
</button>
<button
type="button"
onClick={onResend}
disabled={resendIn > 0}
className="text-brand-600 hover:underline disabled:text-gray-400 disabled:no-underline"
>
{resendIn > 0 ? `Resend code in ${resendIn}s` : "Resend code"}
</button>
{!challenge.enrolling && (
<button type="button" onClick={() => { setUseRecovery(!useRecovery); setCode(""); setError(null); }} className="text-brand-600 hover:underline">
{useRecovery ? "Use a code instead" : "Use a recovery code"}
</button>
)}
{challenge.method !== "authenticator" && !useRecovery && (
<button
type="button"
onClick={onResend}
disabled={resendIn > 0}
className="text-brand-600 hover:underline disabled:text-gray-400 disabled:no-underline"
>
{resendIn > 0 ? `Resend code in ${resendIn}s` : "Resend code"}
</button>
)}
</div>
</form>
);