[PR #7243] fix(two-factor): allow passwordless 2FA management #7168

Open
opened 2026-03-13 13:26:27 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/7243
Author: @gustavovalverde
Created: 1/10/2026
Status: 🔄 Open

Base: canaryHead: feat/two-factor-passwordless-canary


📝 Commits (1)

  • 4fc867a feat(two-factor): allow passwordless 2FA management

📊 Changes

7 files changed (+314 additions, -66 deletions)

View changed files

📝 docs/content/docs/plugins/2fa.mdx (+13 -11)
📝 packages/better-auth/src/plugins/two-factor/backup-codes/index.ts (+33 -8)
📝 packages/better-auth/src/plugins/two-factor/index.ts (+85 -40)
📝 packages/better-auth/src/plugins/two-factor/totp/index.ts (+29 -7)
📝 packages/better-auth/src/plugins/two-factor/two-factor.test.ts (+130 -0)
📝 packages/better-auth/src/plugins/two-factor/types.ts (+7 -0)
📝 packages/better-auth/src/utils/password.ts (+17 -0)

📄 Description

Summary

  • add allowPasswordless to two-factor management so passwordless users can enable/disable 2FA, fetch TOTP URIs, and regenerate backup codes
  • preserve the password requirement for users with credential accounts
  • expand tests for passwordless flows and update docs to list supported passwordless methods explicitly

Rationale

Passwordless sign-in methods (passkeys, magic links, email OTP, OAuth/social, anonymous) cannot provide a password, but they still benefit from 2FA. This change enables 2FA management for those users while maintaining the existing password gate for credential accounts.


Summary by cubic

Adds passwordless 2FA management so users without a credential (password) account can enable/disable 2FA, fetch TOTP URIs, and regenerate backup codes without a password. Credential users still must provide a password; default behavior is unchanged.

  • New Features

    • Added allowPasswordless to twoFactor, TOTP, and backup codes (default false); top-level option cascades to sub-features unless overridden.
    • Endpoints accept optional password when allowed: enableTwoFactor, disableTwoFactor, getTOTPURI, generateBackupCodes.
    • New shouldRequirePassword utility enforces password for credential accounts and when allowPasswordless is off.
    • Updated docs to mark password as optional for passwordless users and added tests for anonymous flows and credential enforcement.
  • Migration

    • No changes needed by default.
    • To enable for passwordless users: twoFactor({ allowPasswordless: true }) or set per-feature options.
    • Clients may omit password only for users without a credential account; keep sending password for credential users.

Written for commit 4fc867aa55. 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/7243 **Author:** [@gustavovalverde](https://github.com/gustavovalverde) **Created:** 1/10/2026 **Status:** 🔄 Open **Base:** `canary` ← **Head:** `feat/two-factor-passwordless-canary` --- ### 📝 Commits (1) - [`4fc867a`](https://github.com/better-auth/better-auth/commit/4fc867aa55c8542dbf52c23814f6280bffe13321) feat(two-factor): allow passwordless 2FA management ### 📊 Changes **7 files changed** (+314 additions, -66 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/plugins/2fa.mdx` (+13 -11) 📝 `packages/better-auth/src/plugins/two-factor/backup-codes/index.ts` (+33 -8) 📝 `packages/better-auth/src/plugins/two-factor/index.ts` (+85 -40) 📝 `packages/better-auth/src/plugins/two-factor/totp/index.ts` (+29 -7) 📝 `packages/better-auth/src/plugins/two-factor/two-factor.test.ts` (+130 -0) 📝 `packages/better-auth/src/plugins/two-factor/types.ts` (+7 -0) 📝 `packages/better-auth/src/utils/password.ts` (+17 -0) </details> ### 📄 Description ## Summary - add `allowPasswordless` to two-factor management so passwordless users can enable/disable 2FA, fetch TOTP URIs, and regenerate backup codes - preserve the password requirement for users with credential accounts - expand tests for passwordless flows and update docs to list supported passwordless methods explicitly ## Rationale Passwordless sign-in methods (passkeys, magic links, email OTP, OAuth/social, anonymous) cannot provide a password, but they still benefit from 2FA. This change enables 2FA management for those users while maintaining the existing password gate for credential accounts. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Adds passwordless 2FA management so users without a credential (password) account can enable/disable 2FA, fetch TOTP URIs, and regenerate backup codes without a password. Credential users still must provide a password; default behavior is unchanged. - **New Features** - Added `allowPasswordless` to `twoFactor`, TOTP, and backup codes (default false); top-level option cascades to sub-features unless overridden. - Endpoints accept optional password when allowed: `enableTwoFactor`, `disableTwoFactor`, `getTOTPURI`, `generateBackupCodes`. - New `shouldRequirePassword` utility enforces password for credential accounts and when `allowPasswordless` is off. - Updated docs to mark password as optional for passwordless users and added tests for anonymous flows and credential enforcement. - **Migration** - No changes needed by default. - To enable for passwordless users: `twoFactor({ allowPasswordless: true })` or set per-feature options. - Clients may omit password only for users without a credential account; keep sending password for credential users. <sup>Written for commit 4fc867aa55c8542dbf52c23814f6280bffe13321. 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-03-13 13:26:28 -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#7168