TOTP-based sign-in protection
Protect accounts with authenticator applications.
Better Auth · Function
Better Auth’s twoFactor plugin adds TOTP and OTP second-factor authentication, along with backup codes and trusted-device support. Register it in the server auth configuration and set an issuer for TOTP enrollment.
The twoFactor plugin adds second-factor authentication to Better Auth. Users can use authenticator-generated TOTP codes or delivery-based OTP codes, with support for backup codes, activation management, and trusted devices.
twoFactor(options?)| Name | Type | Description |
|---|---|---|
options | TwoFactorOptions | Plugin configuration, including the issuer and optional OTP settings described by the documentation. |
A Better Auth two-factor plugin instance; enabling the feature returns method-specific data such as a TOTP URI and backup codes for TOTP, or the selected method for OTP.
sendOTP implementation configured.import { betterAuth } from "better-auth";
import { twoFactor } from "better-auth/plugins";
export const auth = betterAuth({
plugins: [twoFactor({ issuer: "My App" })]
});Register the plugin on the server and provide the application name used as the issuer.
Protect accounts with authenticator applications.
Use email, SMS, or another application-managed delivery channel.
Help users regain access without their usual authenticator.
Avoid prompting repeatedly on recognized hardware.
twoFactor is a Better Auth plugin that adds a second verification step after primary sign-in. It supports TOTP from an authenticator app and OTP delivered through an application-configured channel.
Follow-up: How does the plugin distinguish the setup flows? TOTP returns a URI and backup codes after code verification, whereas OTP enables immediately and returns its method.
The default method is totp, which prepares authenticator-app enrollment and requires the user to verify a generated code. The alternative otp method enables email- or SMS-style verification immediately.
Follow-up: What server dependency does OTP have? The application must configure otpOptions.sendOTP so Better Auth can deliver the code.
After enabling two-factor authentication, the client can use the corresponding verification endpoint during sign-in. The client plugin also accepts a page destination for users who need to complete second-factor verification.
Follow-up: What related client behavior is supported? A successful trusted-device verification can prevent repeated second-factor prompts on that device.
twoFactorClienttwoFactor.enabletwoFactor.disabletwoFactor.verifyTOTPauthClient.twoFactor.sendOtpauth.api.sendTwoFactorOTPsendOTP