Add optional two-factor authentication
Users can turn on 2FA from Profile & Security and receive a 6-digit login code by email or WhatsApp, with a "trust this device for 30 days" option. Admins can reset it for a locked-out user, and the self-service kiosk handles the code step at setup. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||
import { apiFetch } from "@/lib/api";
|
||||
import { TwoFactorChallenge, isTwoFactorChallenge } from "@/lib/auth";
|
||||
|
||||
// ─── Types ────────────────────────────────────────────────────────────────────
|
||||
type EarlyBirdTier = { deadline: string; price: number; order?: number; variantId?: string | null };
|
||||
@@ -133,6 +134,9 @@ export default function SelfServicePage() {
|
||||
const [setupLoading, setSetupLoading] = useState(false);
|
||||
const [setupError, setSetupError] = useState<string | null>(null);
|
||||
const [supervisorToken, setSupervisorToken] = useState<string | null>(null);
|
||||
// Set while a supervisor with two-factor on still has to enter their code.
|
||||
const [setupChallenge, setSetupChallenge] = useState<TwoFactorChallenge | null>(null);
|
||||
const [setupCode, setSetupCode] = useState("");
|
||||
const [events, setEvents] = useState<KioskEvent[]>([]);
|
||||
const [selectedEventId, setSelectedEventId] = useState("");
|
||||
const [eventsLoading, setEventsLoading] = useState(false);
|
||||
@@ -277,16 +281,32 @@ export default function SelfServicePage() {
|
||||
setSetupError(null);
|
||||
setSetupLoading(true);
|
||||
try {
|
||||
const data = await apiFetch("/api/users/login", {
|
||||
method: "POST",
|
||||
body: { email: setupEmail, password: setupPassword },
|
||||
});
|
||||
// Second pass for accounts with two-factor on: exchange the code that was
|
||||
// sent after the password step. The kiosk never asks to be remembered.
|
||||
const data = setupChallenge
|
||||
? await apiFetch("/api/users/login/2fa", {
|
||||
method: "POST",
|
||||
body: { challengeId: setupChallenge.challengeId, code: setupCode, rememberDevice: false },
|
||||
})
|
||||
: await apiFetch("/api/users/login", {
|
||||
method: "POST",
|
||||
body: { email: setupEmail, password: setupPassword },
|
||||
});
|
||||
if (isTwoFactorChallenge(data)) {
|
||||
setSetupChallenge(data);
|
||||
setSetupCode("");
|
||||
return;
|
||||
}
|
||||
const role: string = data.role || "";
|
||||
if (!["admin", "supervisor"].includes(role)) {
|
||||
setSetupError("Only admins and supervisors can set up the kiosk.");
|
||||
setSetupChallenge(null);
|
||||
setSetupCode("");
|
||||
setSetupLoading(false);
|
||||
return;
|
||||
}
|
||||
setSetupChallenge(null);
|
||||
setSetupCode("");
|
||||
setSupervisorToken(data.token);
|
||||
await loadEvents(data.token);
|
||||
} catch (err: any) {
|
||||
@@ -316,18 +336,16 @@ export default function SelfServicePage() {
|
||||
setChangeError(null);
|
||||
setChangeLoading(true);
|
||||
try {
|
||||
const data = await apiFetch("/api/users/login", {
|
||||
// Re-confirm the password against the supervisor session the kiosk already
|
||||
// holds, rather than logging in again — a fresh login would send a new
|
||||
// two-factor code every time the event is changed.
|
||||
if (!supervisorToken) throw new Error("Your session has expired. Reload the kiosk and sign in again.");
|
||||
await apiFetch("/api/users/verify-password", {
|
||||
method: "POST",
|
||||
body: { email: setupEmail, password: changePassword },
|
||||
authToken: supervisorToken,
|
||||
body: { password: changePassword },
|
||||
});
|
||||
const role: string = data.role || "";
|
||||
if (!["admin", "supervisor"].includes(role)) {
|
||||
setChangeError("Only admins and supervisors can change the event.");
|
||||
setChangeLoading(false);
|
||||
return;
|
||||
}
|
||||
setSupervisorToken(data.token);
|
||||
await loadEvents(data.token);
|
||||
await loadEvents(supervisorToken);
|
||||
setChangePassword("");
|
||||
setChangeError(null);
|
||||
setShowChangeModal(false);
|
||||
@@ -701,13 +719,40 @@ export default function SelfServicePage() {
|
||||
<PasswordToggleButton shown={showSetupPassword} onToggle={() => setShowSetupPassword((v) => !v)} />
|
||||
</div>
|
||||
</div>
|
||||
{setupChallenge && (
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-1">Verification code</label>
|
||||
<input
|
||||
type="text"
|
||||
inputMode="numeric"
|
||||
autoComplete="one-time-code"
|
||||
maxLength={6}
|
||||
value={setupCode}
|
||||
onChange={(e) => setSetupCode(e.target.value.replace(/\D/g, ""))}
|
||||
className="w-full border border-gray-300 rounded-xl px-4 py-3 text-base tracking-widest focus:outline-none focus:ring-2 focus:ring-brand-500"
|
||||
placeholder="6-digit code"
|
||||
autoFocus
|
||||
required
|
||||
/>
|
||||
<p className="mt-1 text-xs text-gray-500">
|
||||
We sent a code to {setupChallenge.destination} by {setupChallenge.method === "whatsapp" ? "WhatsApp" : "email"}.{" "}
|
||||
<button
|
||||
type="button"
|
||||
className="text-brand-600 hover:underline"
|
||||
onClick={() => { setSetupChallenge(null); setSetupCode(""); setSetupError(null); }}
|
||||
>
|
||||
Start again
|
||||
</button>
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
{setupError && <p className="text-red-600 text-sm">{setupError}</p>}
|
||||
<button
|
||||
type="submit"
|
||||
disabled={setupLoading}
|
||||
className="w-full bg-brand-600 text-white rounded-xl py-3.5 text-lg font-semibold hover:bg-brand-700 disabled:opacity-60 transition mt-2"
|
||||
>
|
||||
{setupLoading ? "Signing in…" : "Sign In"}
|
||||
{setupLoading ? "Signing in…" : setupChallenge ? "Verify" : "Sign In"}
|
||||
</button>
|
||||
</form>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user