Require two-factor authentication for admins and supervisors

Admins and supervisors without 2FA are sent a code at their next login,
and verifying it turns 2FA on. They can change the channel but not turn
it off. Older sessions without 2FA are rejected with TWO_FACTOR_REQUIRED,
and promoting a user ends their sessions so they enrol at next login.
The requirement is skipped when no email or WhatsApp channel can deliver
a code, and TWO_FACTOR_REQUIRED_ROLES=false switches it off.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-19 11:00:12 +02:00
co-authored by Claude Opus 5.5
parent 0306c4dcb1
commit c3012cb3f6
21 changed files with 356 additions and 55 deletions
+13 -1
View File
@@ -103,7 +103,9 @@ export function LoginForm() {
if (!challenge || resendIn > 0) return;
setError(null);
try {
setChallenge(await resendTwoFactor(challenge.challengeId));
// The resend response doesn't repeat `enrolling`, so carry it over.
const next = await resendTwoFactor(challenge.challengeId);
setChallenge({ ...next, enrolling: challenge.enrolling });
setCode("");
setResendIn(RESEND_COOLDOWN_SECONDS);
} catch (err: any) {
@@ -123,6 +125,11 @@ export function LoginForm() {
<form onSubmit={onSubmitCode} className="space-y-4 max-w-sm w-full">
<div>
<h2 className="text-base font-semibold">Enter your verification code</h2>
{challenge.enrolling && (
<p className="text-sm text-brand-800 bg-brand-50 border border-brand-200 rounded px-3 py-2 mt-2">
Two-factor authentication is required for your role. Entering this code turns it on for your account.
</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.
</p>
@@ -182,6 +189,11 @@ export function LoginForm() {
return (
<form onSubmit={onSubmit} className="space-y-4 max-w-sm w-full">
{searchParams.get("reason") === "2fa" && (
<p className="text-sm text-brand-800 bg-brand-50 border border-brand-200 rounded px-3 py-2">
Two-factor authentication is now required for your role. Log in again to set it up — we&apos;ll send you a code.
</p>
)}
<div>
<label className="block text-sm font-medium mb-1">Email or phone</label>
<input