npx skills add ...
npx skills add better-auth/skills --skill two-factor-authentication-best-practices
Configure TOTP authenticator apps, send OTP codes via email/SMS, manage backup codes, handle trusted devices, and implement 2FA sign-in flows using Better Auth's twoFactor plugin. Use when users need MFA, multi-factor authentication, authenticator setup, or login security with Better Auth.
npx skills add better-auth/skills --skill two-factor-authentication-best-practices
twoFactor() plugin to server config with issuertwoFactorClient() plugin to client confignpx auth@latest migrate (built-in adapter) or generate + push for Drizzle/PrismatwoFactorSecret column exists on user tableRequires password verification. Returns TOTP URI (for QR code) and backup codes.
twoFactorEnabled is not set to true until first TOTP verification succeeds. Override with skipVerificationOnEnable: true (not recommended).
Accepts codes from one period before/after current time:
Send: authClient.twoFactor.sendOtp(). Verify: authClient.twoFactor.verifyOtp({ code, trustDevice: true }).
Configure how OTP codes are stored in the database:
For custom encryption:
Generated automatically when 2FA is enabled. Each code is single-use.
Invalidates all previous codes:
Response includes twoFactorRedirect: true when 2FA is required:
signIn.email({ email, password })context.data.twoFactorRedirect in onSuccesstrue, redirect to /2fa verification pageServer-side: check "twoFactorRedirect" in response when using auth.api.signInEmail.
Pass trustDevice: true when verifying. Default trust duration: 30 days (trustDeviceMaxAge). Refreshes on each sign-in.
Flow: credentials → session removed → temporary 2FA cookie (10 min default) → verify → session created.
Built-in: 3 requests per 10 seconds for all 2FA endpoints. OTP has additional attempt limiting:
TOTP secrets: encrypted with auth secret. Backup codes: encrypted by default. OTP: configurable ("plain", "encrypted", "hashed"). Uses constant-time comparison for verification.
2FA can only be enabled for credential (email/password) accounts.
Requires password confirmation. Revokes trusted device records: