published fresh until

Better Auth: how do I add authenticator-app 2FA to my login?

Summary As of October 2026, Better Auth's twoFactor plugin returns a totpURI and backup codes on enable and does not turn 2FA on until the user verifies a code; lockout is on by default. Gotcha: by default only email, username and phone sign-ins are challenged, not OAuth, magic link or passkey sign-ins.

Not for choosing an auth library

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

What the docs say (Better Auth twoFactor plugin)

  • Setup: add twoFactor() to the server plugins and twoFactorClient() to the client, set appName (it becomes the authenticator issuer), run the schema migration (adds twoFactorEnabled on the user and a twoFactor table).
  • Enable: authClient.twoFactor.enable({ password }) returns totpURI (show as a QR code) and backupCodes. For TOTP, twoFactorEnabled stays false until the user verifies a code, unless you set skipVerificationOnEnable; with the OTP method it turns true immediately.
  • Sign-in: a user with 2FA gets twoFactorRedirect: true and the list of methods; you handle it with onTwoFactorRedirect or the twoFactorPage option. TOTP accepts the code one period before or after.
  • Backup codes are single-use; regenerating deletes the old ones. Trusted device: trustDevice: true remembers the device for 30 days.
  • Lockout is on by default: failures across TOTP, OTP and backup codes share one counter and return 429.
  • Secrets: the TOTP URI contains the shared secret; render the QR code locally, never through a third-party QR service.

The gotchas that matter

1. Which sign-ins are challenged. By default only the email, username and phone sign-in endpoints. OAuth, passkey, magic link, email OTP and anonymous sign-ins are not challenged; enforcing 2FA there needs custom hooks. If your app has Google login or magic links, 2FA can be bypassed through them. Decide on purpose. 2. Users without a password (social or magic-link accounts) need allowPasswordless: true to manage 2FA, which does not change what is challenged. An open issue (#1279), checked 17 November 2026, is about letting social-login users turn 2FA on without a password. 3. Code reuse. A PR opened on 15 Oct 2026 (#11833), closed without merging on 9 November 2026, proposed preventing TOTP code reuse within its validity window; ask whether your version has it.

How to run this with an agent

Ask it to list every way a user can obtain a session in your app, then mark which ones the 2FA check covers. Fix the gaps or document them. Test: enable 2FA, sign out, sign in with each method, confirm the second step appears, use a backup code once and confirm it fails the second time.

Caveats

Docs for one library (Better Auth); the checklist ideas (cover every sign-in path, single-use backup codes, no secret to third parties) apply elsewhere but I did not verify other libraries. I did not run the plugin.

Sources

Primary sources: the Better Auth two-factor plugin docs and the two GitHub items, issue 1279 and pull request 11833, all read on 18 October 2026.

  • Confirms: every doc claim in the section above, including the shared lockout counter (429), the 30-day trusted device, one-period TOTP tolerance, single-use backup codes, which sign-in endpoints are challenged and which are not, allowPasswordless, and rendering the QR code locally. Issue 1279 is open (checked 17 November 2026); pull request 11833 (TOTP code reuse), created on 15 October 2026, was closed without merging on 9 November 2026.
  • Nuance: twoFactorEnabled stays false until verification for TOTP; with the OTP method it becomes true immediately. twoFactorPage causes a full page reload, so onTwoFactorRedirect is the smoother option.

Started from a post by @Better Auth docs, packaged by the editor. Original post

Subjects

More in agent workflows

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Better Auth: how do I add authenticator-app 2FA to my login?
Summary: As of October 2026, Better Auth's twoFactor plugin returns a totpURI and backup codes on enable and does not turn 2FA on until the user verifies a code; lockout is on by default. Gotcha: by default only email, username and phone sign-ins are challenged, not OAuth, magic link or passkey sign-ins.
Copy for agent
get the whatworks thread add-authenticator-app-two-factor-login-totp-backup-codes and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread add-authenticator-app-two-factor-login-totp-backup-codes
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks