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, aut...
Frontmatter
name two-factor-authentication-best-practices
description 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. Setup
Add twoFactor() plugin to server config with issuer
Add twoFactorClient() plugin to client config
Run npx auth@latest migrate (built-in adapter) or generate + push for Drizzle/Prisma
Verify: check that twoFactorSecret column exists on user table
import { betterAuth } from "better-auth" ;
import { twoFactor } from "better-auth/plugins" ;
export const auth = betterAuth ({
appName : "My App" ,
plugins : [
twoFactor ({
issuer : "My App" ,
}),
],
});
Client-Side Setup
import { createAuthClient } from "better-auth/client" ;
import { twoFactorClient } from "better-auth/client/plugins" ;
export const authClient = createAuthClient ({
plugins : [
twoFactorClient ({
onTwoFactorRedirect ( ) {
window .location .href = "/2fa" ;
},
}),
],
});
Enabling 2FA for Users
Requires password verification. Returns TOTP URI (for QR code) and backup codes.
const enable2FA = async (password : string ) => {
const { data, error } = await authClient.twoFactor .enable ({
password,
});
if (data) {
}
};
twoFactorEnabled is not set to true until first TOTP verification succeeds. Override with skipVerificationOnEnable: true (not recommended).
TOTP (Authenticator App)
Displaying the QR Code
import QRCode from "react-qr-code" ;
const TotpSetup = ({ totpURI }: { totpURI: string } ) => {
return <QRCode value ={totpURI} /> ;
};
Verifying TOTP Codes
Accepts codes from one period before/after current time:
const verifyTotp = async (code : string ) => {
const { data, error } = await authClient.twoFactor .verifyTotp ({
code,
trustDevice : true ,
});
};
TOTP Configuration Options
twoFactor ({
totpOptions : {
digits : 6 ,
period : 30 ,
},
});
OTP (Email/SMS)
Configuring OTP Delivery
import { betterAuth } from "better-auth" ;
import { twoFactor } from "better-auth/plugins" ;
import { sendEmail } from "./email" ;
export const auth = betterAuth ({
plugins : [
twoFactor ({
otpOptions : {
sendOTP : async ({ user, otp }, ctx) => {
await sendEmail ({
to : user.email ,
subject : "Your verification code" ,
text : `Your code is: ${otp} ` ,
});
},
period : 5 ,
digits : 6 ,
allowedAttempts : 5 ,
},
}),
],
});
Sending and Verifying OTP
Send: authClient.twoFactor.sendOtp(). Verify: authClient.twoFactor.verifyOtp({ code, trustDevice: true }).
OTP Storage Security
Configure how OTP codes are stored in the database:
twoFactor ({
otpOptions : {
storeOTP : "encrypted" ,
},
});
For custom encryption:
twoFactor ({
otpOptions : {
storeOTP : {
encrypt : async (token) => myEncrypt (token),
decrypt : async (token) => myDecrypt (token),
},
},
});
Backup Codes
Generated automatically when 2FA is enabled. Each code is single-use.
Displaying Backup Codes
const BackupCodes = ({ codes }: { codes: string [] } ) => {
return (
<div >
<p > Save these codes in a secure location:</p >
<ul >
{codes.map((code, i) => (
<li key ={i} > {code}</li >
))}
</ul >
</div >
);
};
Regenerating Backup Codes
Invalidates all previous codes:
const regenerateBackupCodes = async (password : string ) => {
const { data, error } = await authClient.twoFactor .generateBackupCodes ({
password,
});
};
Using Backup Codes for Recovery
const verifyBackupCode = async (code : string ) => {
const { data, error } = await authClient.twoFactor .verifyBackupCode ({
code,
trustDevice : true ,
});
};
Backup Code Configuration
twoFactor ({
backupCodeOptions : {
amount : 10 ,
length : 10 ,
storeBackupCodes : "encrypted" ,
},
});
Handling 2FA During Sign-In
Response includes twoFactorRedirect: true when 2FA is required:
Sign-In Flow
Call signIn.email({ email, password })
Check context.data.twoFactorRedirect in onSuccess
If true, redirect to /2fa verification page
Verify via TOTP, OTP, or backup code
Session cookie is created on successful verification
const signIn = async (email : string , password : string ) => {
const { data, error } = await authClient.signIn .email (
{ email, password },
{
onSuccess (context ) {
if (context.data .twoFactorRedirect ) {
window .location .href = "/2fa" ;
}
},
}
);
};
Server-side: check "twoFactorRedirect" in response when using auth.api.signInEmail.
Trusted Devices
Pass trustDevice: true when verifying. Default trust duration: 30 days (trustDeviceMaxAge). Refreshes on each sign-in.
Security Considerations
Session Management
Flow: credentials → session removed → temporary 2FA cookie (10 min default) → verify → session created.
twoFactor ({
twoFactorCookieMaxAge : 600 ,
});
Rate Limiting
Built-in: 3 requests per 10 seconds for all 2FA endpoints. OTP has additional attempt limiting:
twoFactor ({
otpOptions : {
allowedAttempts : 5 ,
},
});
Encryption at Rest
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.
Disabling 2FA
Requires password confirmation. Revokes trusted device records:
const disable2FA = async (password : string ) => {
const { data, error } = await authClient.twoFactor .disable ({
password,
});
};
Complete Configuration Example
import { betterAuth } from "better-auth" ;
import { twoFactor } from "better-auth/plugins" ;
import { sendEmail } from "./email" ;
export const auth = betterAuth ({
appName : "My App" ,
plugins : [
twoFactor ({
issuer : "My App" ,
totpOptions : {
digits : 6 ,
period : 30 ,
},
otpOptions : {
sendOTP : async ({ user, otp }) => {
await sendEmail ({
to : user.email ,
subject : "Your verification code" ,
text : `Your code is: ${otp} ` ,
});
},
period : 5 ,
allowedAttempts : 5 ,
storeOTP : "encrypted" ,
},
backupCodeOptions : {
amount : 10 ,
length : 10 ,
storeBackupCodes : "encrypted" ,
},
twoFactorCookieMaxAge : 600 ,
trustDeviceMaxAge : 30 * 24 * 60 * 60 ,
}),
],
});