Two-Factor Authentication (2FA)

OTP TOTP Backup Codes Trusted Devices

Two-Factor Authentication (2FA) adds an extra security step when users log in. Instead of just using a password, they'll need to provide a second form of verification. This makes it much harder for unauthorized people to access accounts, even if they've somehow gotten the password.

This plugin offers two main methods to do a second factor verification:

  1. OTP (One-Time Password): A temporary code sent to the user's email or phone.
  2. TOTP (Time-based One-Time Password): A code generated by an app on the user's device.

Additional features include:

  • Generating backup codes for account recovery
  • Enabling/disabling 2FA
  • Managing trusted devices

Installation

Add the plugin to your auth config

Add the two-factor plugin to your auth configuration and specify your app name as the issuer.

auth.ts
import { betterAuth } from "better-auth"
import { twoFactor } from "better-auth/plugins"
 
export const auth = betterAuth({
    // ... other config options
    appName: "My App", // provide your app name. It'll be used as an issuer.
    plugins: [
        twoFactor() 
    ]
})

Migrate the database

Run the migration or generate the schema to add the necessary fields and tables to the database.

npx @better-auth/cli migrate

See the Schema section to add the fields manually.

Add the client plugin

Add the client plugin and Specify where the user should be redirected if they need to verify 2nd factor

auth-client.ts
import { createAuthClient } from "better-auth/client"
import { twoFactorClient } from "better-auth/client/plugins"
 
export const authClient = createAuthClient({
    plugins: [
        twoFactorClient()
    ]
})

Usage

Enabling 2FA

To enable two-factor authentication, call twoFactor.enable with the user's password and issuer (optional):

POST
/two-factor/enable
const { data, error } = await authClient.twoFactor.enable({
    password: "secure-password", // required
    issuer: "my-app-name",
});
PropDescriptionType
password
The user's password
string
issuer?
An optional custom issuer for the TOTP URI. Defaults to app-name defined in your auth config.
string

When 2FA is enabled:

  • An encrypted secret and backupCodes are generated.
  • enable returns totpURI and backupCodes.

Note: twoFactorEnabled won’t be set to true until the user verifies their TOTP code. Learn more about veryifying TOTP here. You can skip verification by setting skipVerificationOnEnable to true in your plugin config.

Two Factor can only be enabled for credential accounts at the moment. For social accounts, it's assumed the provider already handles 2FA.

Sign In with 2FA

When a user with 2FA enabled tries to sign in via email, the response object will contain twoFactorRedirect set to true. This indicates that the user needs to verify their 2FA code.

You can handle this in the onSuccess callback or by providing a onTwoFactorRedirect callback in the plugin config.

sign-in.tsx
await authClient.signIn.email({
        email: "user@example.com",
        password: "password123",
    },
    {
        async onSuccess(context) {
            if (context.data.twoFactorRedirect) {
                // Handle the 2FA verification in place
            }
        },
    }
)

Using the onTwoFactorRedirect config:

sign-in.ts
import { createAuthClient } from "better-auth/client";
import { twoFactorClient } from "better-auth/client/plugins";
 
const authClient = createAuthClient({
    plugins: [
        twoFactorClient({
            onTwoFactorRedirect(){
                // Handle the 2FA verification globally
            },
        }),
    ],
});

With auth.api

When you call auth.api.signInEmail on the server, and the user has 2FA enabled, it will return an object where twoFactorRedirect is set to true. This behavior isn’t inferred in TypeScript, which can be misleading. You can check using in instead to check if twoFactorRedirect is set to true.

const response = await auth.api.signInEmail({
	body: {
		email: "test@test.com",
		password: "test",
	},
});
 
if ("twoFactorRedirect" in response) {
	// Handle the 2FA verification in place
}

Disabling 2FA

To disable two-factor authentication, call twoFactor.disable with the user's password:

POST
/two-factor/disable
const { data, error } = await authClient.twoFactor.disable({
    password, // required
});
PropDescriptionType
password
The user's password
string

TOTP

TOTP (Time-Based One-Time Password) is an algorithm that generates a unique password for each login attempt using time as a counter. Every fixed interval (Better Auth defaults to 30 seconds), a new password is generated. This addresses several issues with traditional passwords: they can be forgotten, stolen, or guessed. OTPs solve some of these problems, but their delivery via SMS or email can be unreliable (or even risky, considering it opens new attack vectors).

TOTP, however, generates codes offline, making it both secure and convenient. You just need an authenticator app on your phone.

Getting TOTP URI

After enabling 2FA, you can get the TOTP URI to display to the user. This URI is generated by the server using the secret and issuer and can be used to generate a QR code for the user to scan with their authenticator app.

POST
/two-factor/get-totp-uri
const { data, error } = await authClient.twoFactor.getTotpUri({
    password, // required
});
PropDescriptionType
password
The user's password
string

Example: Using React

Once you have the TOTP URI, you can use it to generate a QR code for the user to scan with their authenticator app.

user-card.tsx
import QRCode from "react-qr-code";
 
export default function UserCard({ password }: { password: string }){
    const { data: session } = client.useSession();
	const { data: qr } = useQuery({
		queryKey: ["two-factor-qr"],
		queryFn: async () => {
			const res = await authClient.twoFactor.getTotpUri({ password });
			return res.data;
		},
		enabled: !!session?.user.twoFactorEnabled,
	});
    return (
        <QRCode value={qr?.totpURI || ""} />
   )
}

By default the issuer for TOTP is set to the app name provided in the auth config or if not provided it will be set to Better Auth. You can override this by passing issuer to the plugin config.

Verifying TOTP

After the user has entered their 2FA code, you can verify it using twoFactor.verifyTotp method.

POST
/two-factor/verify-totp
const { data, error } = await authClient.twoFactor.verifyTotp({
    code: "012345", // required
    trustDevice: true,
});
PropDescriptionType
code
The otp code to verify.
string
trustDevice?
If true, the device will be trusted for 30 days. It'll be refreshed on every sign in request within this time.
boolean

OTP

OTP (One-Time Password) is similar to TOTP but a random code is generated and sent to the user's email or phone.

Before using OTP to verify the second factor, you need to configure sendOTP in your Better Auth instance. This function is responsible for sending the OTP to the user's email, phone, or any other method supported by your application.

auth.ts
import { betterAuth } from "better-auth"
import { twoFactor } from "better-auth/plugins"
 
export const auth = betterAuth({
    plugins: [
        twoFactor({
          	otpOptions: {
				async sendOTP({ user, otp }, request) {
                    // send otp to user
				},
			},
        })
    ]
})

Sending OTP

Sending an OTP is done by calling the twoFactor.sendOtp function. This function will trigger your sendOTP implementation that you provided in the Better Auth configuration.

POST
/two-factor/send-otp
const { data, error } = await authClient.twoFactor.sendOtp({
    trustDevice: true,
});
 
if (data) {
    // redirect or show the user to enter the code
}
PropDescriptionType
trustDevice?
If true, the device will be trusted for 30 days. It'll be refreshed on every sign in request within this time.
boolean

Verifying OTP

After the user has entered their OTP code, you can verify it

POST
/two-factor/verify-otp
const { data, error } = await authClient.twoFactor.verifyOtp({
    code: "012345", // required
    trustDevice: true,
});
PropDescriptionType
code
The otp code to verify.
string
trustDevice?
If true, the device will be trusted for 30 days. It'll be refreshed on every sign in request within this time.
boolean

Backup Codes

Backup codes are generated and stored in the database. This can be used to recover access to the account if the user loses access to their phone or email.

Generating Backup Codes

Generate backup codes for account recovery:

POST
/two-factor/generate-backup-codes
const { data, error } = await authClient.twoFactor.generateBackupCodes({
    password, // required
});
 
if (data) {
    // Show the backup codes to the user
}
PropDescriptionType
password
The users password.
string

When you generate backup codes, the old backup codes will be deleted and new ones will be generated.

Using Backup Codes

You can now allow users to provider backup code as account recover method.

POST
/two-factor/verify-backup-code
const { data, error } = await authClient.twoFactor.verifyBackupCode({
    code: "123456", // required
    disableSession: false,
    trustDevice: true,
});
PropDescriptionType
code
A backup code to verify.
string
disableSession?
If true, the session cookie will not be set.
boolean
trustDevice?
If true, the device will be trusted for 30 days. It'll be refreshed on every sign in request within this time.
boolean

Once a backup code is used, it will be removed from the database and can't be used again.

Viewing Backup Codes

To display the backup codes to the user, you can call viewBackupCodes on the server. This will return the backup codes in the response. You should only this if the user has a fresh session - a session that was just created.

GET
/two-factor/view-backup-codes
const data = await auth.api.viewBackupCodes({
    body: {
        userId: "user-id",
    },
});
PropDescriptionType
userId?
The user ID to view all backup codes.
string | null

Trusted Devices

You can mark a device as trusted by passing trustDevice to verifyTotp or verifyOtp.

const verify2FA = async (code: string) => {
    const { data, error } = await authClient.twoFactor.verifyTotp({
        code,
        callbackURL: "/dashboard",
        trustDevice: true // Mark this device as trusted
    })
    if (data) {
        // 2FA verified and device trusted
    }
}

When trustDevice is set to true, the current device will be remembered for 60 days. During this period, the user won't be prompted for 2FA on subsequent sign-ins from this device. The trust period is refreshed each time the user signs in successfully.

Issuer

By adding an issuer you can set your application name for the 2fa application.

For example, if your user uses Google Auth, the default appName will show up as Better Auth. However, by using the following code, it will show up as my-app-name.

twoFactor({
    issuer: "my-app-name"
})

Schema

The plugin requires 1 additional fields in the user table and 1 additional table to store the two factor authentication data.

Field NameTypeKeyDescription
twoFactorEnabledbooleanWhether two factor authentication is enabled for the user.

Table: twoFactor

Field NameTypeKeyDescription
idstringThe ID of the two factor authentication.
userIdstringThe ID of the user
secretstringThe secret used to generate the TOTP code.
backupCodesstringThe backup codes used to recover access to the account if the user loses access to their phone or email.

Options

Server

twoFactorTable: The name of the table that stores the two factor authentication data. Default: twoFactor.

skipVerificationOnEnable: Skip the verification process before enabling two factor for a user.

Issuer: The issuer is the name of your application. It's used to generate TOTP codes. It'll be displayed in the authenticator apps.

TOTP options

these are options for TOTP.

PropTypeDefault
digits?
number
6
period?
number
30

OTP options

these are options for OTP.

PropTypeDefault
sendOTP?
function
-
period?
number
3
storeOTP?
string
plain

Backup Code Options

backup codes are generated and stored in the database when the user enabled two factor authentication. This can be used to recover access to the account if the user loses access to their phone or email.

PropTypeDefault
amount?
number
10
length?
number
10
customBackupCodesGenerate?
function
-
storeBackupCodes?
string
plain

Client

To use the two factor plugin in the client, you need to add it on your plugins list.

auth-client.ts
import { createAuthClient } from "better-auth/client"
import { twoFactorClient } from "better-auth/client/plugins"
 
const authClient =  createAuthClient({
    plugins: [
        twoFactorClient({ 
            onTwoFactorRedirect(){ 
                window.location.href = "/2fa" // Handle the 2FA verification redirect
            } 
        }) 
    ]
})

Options

onTwoFactorRedirect: A callback that will be called when the user needs to verify their 2FA code. This can be used to redirect the user to the 2FA page.