[PR #8772] [MERGED] feat(two-factor): include enabled 2fa methods in sign-in redirect response #25105

Closed
opened 2026-04-15 22:43:34 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/8772
Author: @aarmful
Created: 3/25/2026
Status: ✅ Merged
Merged: 4/9/2026
Merged by: @gustavovalverde

Base: main ← Head: feat/two-factor-methods


📝 Commits (3)

  • d70099d feat(two-factor): include enabled 2fa methods in sign-in redirect
  • 7323a12 fix(two-factor): return twoFactorMethods based on user state
  • 35356a2 fix(two-factor): exclude unverified TOTP from twoFactorMethods

📊 Changes

5 files changed (+300 additions, -7 deletions)

View changed files

➕ .changeset/two-factor-methods-redirect.md (+11 -0)
📝 docs/content/docs/plugins/2fa.mdx (+9 -5)
📝 packages/better-auth/src/plugins/two-factor/client.ts (+14 -2)
📝 packages/better-auth/src/plugins/two-factor/index.ts (+31 -0)
📝 packages/better-auth/src/plugins/two-factor/two-factor.test.ts (+235 -0)

📄 Description

closes #4101

when a user with 2fa signs in, the redirect response used to only return { twoFactorRedirect: true } with no way to know whether they had totp, otp, or both. frontends had to guess or do hacky workarounds to figure out which verification ui to show.

now the response includes twoFactorMethods (e.g. ["totp", "otp"]), and the onTwoFactorRedirect client callback receives it as a context param.


Summary by cubic

Include twoFactorMethods in the 2FA sign-in redirect and pass it to onTwoFactorRedirect so UIs can show the right flow. Methods are based on verified TOTP status and otpOptions.sendOTP, and exclude disabled or unverified providers.

  • New Features
    • twoFactor in better-auth now returns { twoFactorRedirect: true, twoFactorMethods: [...] }: adds "totp" only if the user has a verified TOTP secret and TOTP isn’t disabled; adds "otp" when otpOptions.sendOTP is set.
    • twoFactorClient calls onTwoFactorRedirect({ twoFactorMethods }). Docs updated; tests cover TOTP/OTP combinations, disabled-TOTP, and unverified-TOTP exclusion.

Written for commit 35356a2bc8. Summary will update on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/8772 **Author:** [@aarmful](https://github.com/aarmful) **Created:** 3/25/2026 **Status:** ✅ Merged **Merged:** 4/9/2026 **Merged by:** [@gustavovalverde](https://github.com/gustavovalverde) **Base:** `main` ← **Head:** `feat/two-factor-methods` --- ### 📝 Commits (3) - [`d70099d`](https://github.com/better-auth/better-auth/commit/d70099df5d237c5f0b4d297dcead7926a30e5a91) feat(two-factor): include enabled 2fa methods in sign-in redirect - [`7323a12`](https://github.com/better-auth/better-auth/commit/7323a123b1f3f696f8003c8db0e7c45cb6c18013) fix(two-factor): return twoFactorMethods based on user state - [`35356a2`](https://github.com/better-auth/better-auth/commit/35356a2bc87f67c2a8cc9750534768e6c6630258) fix(two-factor): exclude unverified TOTP from twoFactorMethods ### 📊 Changes **5 files changed** (+300 additions, -7 deletions) <details> <summary>View changed files</summary> ➕ `.changeset/two-factor-methods-redirect.md` (+11 -0) 📝 `docs/content/docs/plugins/2fa.mdx` (+9 -5) 📝 `packages/better-auth/src/plugins/two-factor/client.ts` (+14 -2) 📝 `packages/better-auth/src/plugins/two-factor/index.ts` (+31 -0) 📝 `packages/better-auth/src/plugins/two-factor/two-factor.test.ts` (+235 -0) </details> ### 📄 Description closes #4101 when a user with 2fa signs in, the redirect response used to only return `{ twoFactorRedirect: true }` with no way to know whether they had totp, otp, or both. frontends had to guess or do hacky workarounds to figure out which verification ui to show. now the response includes `twoFactorMethods` (e.g. `["totp", "otp"]`), and the `onTwoFactorRedirect` client callback receives it as a context param. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Include `twoFactorMethods` in the 2FA sign-in redirect and pass it to `onTwoFactorRedirect` so UIs can show the right flow. Methods are based on verified TOTP status and `otpOptions.sendOTP`, and exclude disabled or unverified providers. - New Features - `twoFactor` in `better-auth` now returns `{ twoFactorRedirect: true, twoFactorMethods: [...] }`: adds `"totp"` only if the user has a verified TOTP secret and TOTP isn’t disabled; adds `"otp"` when `otpOptions.sendOTP` is set. - `twoFactorClient` calls `onTwoFactorRedirect({ twoFactorMethods })`. Docs updated; tests cover TOTP/OTP combinations, disabled-TOTP, and unverified-TOTP exclusion. <sup>Written for commit 35356a2bc87f67c2a8cc9750534768e6c6630258. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-15 22:43:34 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#25105