Multi-Factor Authentication
Two factor kinds
TOTP — a standard authenticator app, RFC 6238, six digits on a thirty-second step — and backup codes, ten single-use strings issued at enrolment for when the authenticator is gone.
Read this before you build the second-factor screen
mfaToken. Resubmitting it fails forever. Send the person back to the first factor and start again. A retry loop on the same token is how you build a permanent lockout on a screen the user cannot dismiss.Signing in with a second factor
const first = await auth.attempt({
identifier: 'ada@example.com',
strategy: 'password',
password,
});
if (first.mfaRequired) {
// first.session is null. first.mfaToken is single-use and lives 5 minutes.
const done = await auth.attempt({
identifier: 'ada@example.com',
strategy: 'totp', // or 'backup_code'
code: sixDigits,
mfaToken: first.mfaToken,
});
use(done.session);
}The mfaToken is what proves the first factor was accepted. Without it the server cannot tell "this person just proved their password" from "this person knows only a TOTP code" — so a second factor submitted alone is refused, as it must be.
Enrolling
// 1. Start enrolment. Returns { factorId, secret, provisioningUri } —
// render provisioningUri as a QR code, or show the secret for manual entry.
const enrolment = await auth.factors.enrolTotp('Acme');
// 2. The user scans it, then proves it works. Enrolment is NOT complete until
// this succeeds — an unconfirmed factor would lock them out.
// The BACKUP CODES come back HERE, not from enrolTotp: they are issued only
// once the factor is proven, because a backup code is a full second factor.
const { verified, backupCodes } = await auth.factors.confirmTotp({
factorId: enrolment.factorId,
code: sixDigits,
});
// 3. Show backupCodes ONCE. They are stored hashed and cannot be shown
// again — only regenerated.Other methods: auth.factors.list(), auth.factors.regenerateBackupCodes() (invalidates the previous set), and auth.factors.remove(factorId).
Backup codes are not optional infrastructure
An authenticator lives on exactly one device, and that device gets lost, wiped and replaced. Without backup codes, that is an account you have to recover by hand. Show them at enrolment, make the user acknowledge them, and put a use a backup code affordance on your second-factor screen — a screen that only accepts TOTP is a lockout waiting for a broken phone.
Requiring MFA project-wide
Project Settings → Auth → Require two-factor authentication. With it on, a user without a factor is required to enrol before their session becomes fully useful, rather than being locked out at the door.
The token records factor ages in the fva claim — minutes since the first and second factor, with -1 when there was no second. Use it for step-up decisions: require a recent second factor before a destructive action, without forcing one on every page.
Details worth knowing
- Codes are accepted one step either side of now (±30 s), absorbing ordinary clock drift.
- Verification is constant-time regardless of which step matched, so timing cannot narrow the window.
- The challenge lives five minutes.
- Secrets are 160-bit, the RFC 4226 recommendation.
- Backup codes are stored hashed. A lost set is regenerated, never recovered.
Next
- Passkeys — a second factor and a first factor at once.
- Sessions and tokens
Was this page helpful?
Or rate this page: