Two-factor
createAuth({ /* … */ twoFactor: { issuer: "Leads" } }); // or twoFactor: trueCodes are TOTP (RFC 6238): 6 digits, 30 seconds, SHA-1. They work with Google Authenticator, 1Password, Authy and the others. The secret is stored encrypted with AES-256-GCM.
Turn it on (settings page)
const started = await auth.twoFactor.setup();
// { ok: true, secret: "JBSW…", uri: "otpauth://totp/Leads:me@x.dev?…" }setup needs a recent sign-in or { password } (see Account settings). Show uri as a QR code (your app draws it) and secret for manual entry. Then ask for a code:
const result = await auth.twoFactor.enable({ code });
// { ok: true, recoveryCodes: ["ABCDE-FGHIJ", …10] } | { ok: false, error: "invalid_code" }Show the recovery codes once and ask the user to save them. enable signs out the user's other sessions.
Sign in with a code
When a user with 2FA on signs in, password.signIn returns { ok: true, next: "two_factor", redirectTo: pages.twoFactor }. Google and GitHub redirect to pages.twoFactor too. No session exists yet; a short challenge cookie (10 minutes) remembers the first step.
On your code page:
const result = await auth.twoFactor.verify({ code });
if (result.ok) navigate({ href: result.redirectTo });
// errors: invalid_code, challenge_expired- The code can be a TOTP code or a recovery code.
- Each TOTP code and each recovery code works once.
- After 5 wrong codes the challenge ends with
challenge_expired. The user signs in again. - Across all challenges, a user gets at most 10 code attempts in 15 minutes and 30 a day (
rate_limited), so new sign-ins do not give an attacker more guesses.
Manage it
await auth.twoFactor.status(); // { enabled, recoveryCodesLeft }
await auth.twoFactor.regenerateRecoveryCodes({ code }); // new codes; the old ones stop working
await auth.twoFactor.disable({ code });disable and regenerateRecoveryCodes need a current code or a recovery code.