Multi-factor authentication
MFA is opt-in per project: Authentication → Settings → Multi-factor authentication. Users add an authenticator app (TOTP, 6 digits, 30 s). Once they did, every sign-in method asks for a code: password, email OTP, magic link, OAuth, SSO and invitation. Only anonymous sign-in is exempt (an anonymous user cannot enroll a factor). Passkey sign-in is already two factors and is aal2 on its own, see Passkeys.
If the user has a verified factor, the sign-in answers mfaRequired, a ticket and the available factors instead of a session. The ticket is valid for 5 minutes. For redirect flows (magic link, OAuth, SSO) it arrives from exchangeCodeForSession after the redirect, so the redirect itself does not change.
// enroll (signed in): render data.totp.uri as a QR code, or show data.totp.secretconst { data: f } = await base.auth.mfa.enroll({ friendlyName: "Phone" })const { data: v } = await base.auth.mfa.verify({ factorId: f!.id, code: "123456" })showOnce(v!.recoveryCodes) // 10 single-use codes, only returned here
// sign in with any method, then the second stepconst r = await base.auth.signInWithPassword({ email, password })if (r.data.mfaRequired) { await base.auth.mfa.challenge({ ticket: r.data.ticket!, code }) // or { recoveryCode }}
// redirect flows end in exchangeCodeForSession, which can also answer mfaRequiredconst x = await base.auth.exchangeCodeForSession(code)if (x.data.mfaRequired) { await base.auth.mfa.challenge({ ticket: x.data.ticket!, code: totp })}
// raise a session that is already signed in from aal1 to aal2const { data: aal } = await base.auth.mfa.getAuthenticatorAssuranceLevel()if (aal!.currentLevel !== aal!.nextLevel) await base.auth.mfa.verify({ code })
await base.auth.mfa.listFactors() // never returns secretsawait base.auth.mfa.unenroll({ factorId }) // a verified factor needs an aal2 sessionawait base.auth.mfa.regenerateRecoveryCodes() // aal2 sessionAll MFA calls return { data, error }; error.code is mfa_disabled until the project enables MFA.
Enforce it with RLS
Section titled “Enforce it with RLS”Tokens carry aal (aal1 or aal2). Enforce MFA on the data, not only in the app:
create policy "payouts need MFA" on public.payouts as restrictive for insert to authenticated with check ((select auth.aal()) = 'aal2');The dashboard policy editor has a Require MFA (aal2) option in Custom mode.
- The level belongs to the session: a refresh keeps
aal2and never raises it. Turning MFA off for the project stops the second step for every method. - Clients that call
/auth/v1directly (/verify,/invite/accept,/token?grant_type=pkce) must handle themfa_requiredresponse. The SDK already does. - With “required for all” users, sessions of users without a factor carry
mfa_enrollment_required: trueso you can prompt them to enroll. Enforcement on data is still youraal2policy. - A code cannot be used twice (
mfa_code_reused). Five wrong codes in five minutes lock the second step for five minutes (429, doubling up to one hour). Recovery codes count toward the same limit. - Lost device: open the user in Authentication → Users and choose Reset MFA.