[PR #7015] feat(two-factor): improve two-factor with trust device and state options #32624

Open
opened 2026-04-17 23:23:21 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/7015
Author: @Bekacru
Created: 12/27/2025
Status: 🔄 Open

Base: nextHead: feat/2fa


📝 Commits (1)

  • d868375 feat(two-factor): improve two-factor with trust device and state options

📊 Changes

16 files changed (+2307 additions, -829 deletions)

View changed files

📝 docs/content/docs/plugins/2fa.mdx (+82 -0)
📝 packages/better-auth/src/plugins/two-factor/backup-codes/index.ts (+7 -2)
📝 packages/better-auth/src/plugins/two-factor/index.ts (+172 -61)
📝 packages/better-auth/src/plugins/two-factor/otp/index.ts (+18 -11)
packages/better-auth/src/plugins/two-factor/tests/backup-codes.test.ts (+203 -0)
packages/better-auth/src/plugins/two-factor/tests/trust-device.test.ts (+330 -0)
packages/better-auth/src/plugins/two-factor/tests/two-factor-api.test.ts (+350 -0)
packages/better-auth/src/plugins/two-factor/tests/two-factor-basic.test.ts (+194 -0)
packages/better-auth/src/plugins/two-factor/tests/two-factor-config.test.ts (+158 -0)
packages/better-auth/src/plugins/two-factor/tests/two-factor-security.test.ts (+262 -0)
packages/better-auth/src/plugins/two-factor/tests/two-factor-storage.test.ts (+185 -0)
packages/better-auth/src/plugins/two-factor/tests/two-factor-test-utils.ts (+209 -0)
📝 packages/better-auth/src/plugins/two-factor/totp/index.ts (+5 -2)
packages/better-auth/src/plugins/two-factor/two-factor.test.ts (+0 -740)
📝 packages/better-auth/src/plugins/two-factor/types.ts (+73 -1)
📝 packages/better-auth/src/plugins/two-factor/verify-two-factor.ts (+59 -12)

📄 Description

  • Added trust device functionality allowing users to mark devices as trusted, skipping 2FA on subsequent logins.
  • Introduced configuration options for trust device, including cookie name and max age.
  • Enhanced two-factor state management with options for storage strategy (cookie, database, or both) and state validity duration.
  • Updated documentation to reflect new features and usage examples.
  • Removed outdated test file for two-factor plugin.

Summary by cubic

Adds “trust this device” to 2FA and configurable 2FA state storage, reducing repeat prompts on known devices and giving apps control over where state lives (cookie, database, or both).

  • New Features

    • Trust device: optional cookie with configurable name and maxAge; skips 2FA on subsequent logins from that device.
    • Two-factor state options: choose storeStrategy (cookie | database | cookieAndDatabase), cookieName, and maxAge.
    • verifyTwoFactor now supports options (cookie name, trust device settings, store strategy, verification token).
    • TOTP and backup-code modules accept an injected verifyTwoFactor for better composition and testing.
    • Docs updated with examples and configuration.
  • Migration

    • Update OTP send handler signature:
      • Before: sendOTP({ user, otp }, request)
      • Now: sendOTP({ user, otp }, ctx) where ctx.query.otpDeliveryMethod is available.

Written for commit d8683756e2. Summary will update automatically 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/7015 **Author:** [@Bekacru](https://github.com/Bekacru) **Created:** 12/27/2025 **Status:** 🔄 Open **Base:** `next` ← **Head:** `feat/2fa` --- ### 📝 Commits (1) - [`d868375`](https://github.com/better-auth/better-auth/commit/d8683756e26599fa67a30cda834d0c48d1b83774) feat(two-factor): improve two-factor with trust device and state options ### 📊 Changes **16 files changed** (+2307 additions, -829 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/plugins/2fa.mdx` (+82 -0) 📝 `packages/better-auth/src/plugins/two-factor/backup-codes/index.ts` (+7 -2) 📝 `packages/better-auth/src/plugins/two-factor/index.ts` (+172 -61) 📝 `packages/better-auth/src/plugins/two-factor/otp/index.ts` (+18 -11) ➕ `packages/better-auth/src/plugins/two-factor/tests/backup-codes.test.ts` (+203 -0) ➕ `packages/better-auth/src/plugins/two-factor/tests/trust-device.test.ts` (+330 -0) ➕ `packages/better-auth/src/plugins/two-factor/tests/two-factor-api.test.ts` (+350 -0) ➕ `packages/better-auth/src/plugins/two-factor/tests/two-factor-basic.test.ts` (+194 -0) ➕ `packages/better-auth/src/plugins/two-factor/tests/two-factor-config.test.ts` (+158 -0) ➕ `packages/better-auth/src/plugins/two-factor/tests/two-factor-security.test.ts` (+262 -0) ➕ `packages/better-auth/src/plugins/two-factor/tests/two-factor-storage.test.ts` (+185 -0) ➕ `packages/better-auth/src/plugins/two-factor/tests/two-factor-test-utils.ts` (+209 -0) 📝 `packages/better-auth/src/plugins/two-factor/totp/index.ts` (+5 -2) ➖ `packages/better-auth/src/plugins/two-factor/two-factor.test.ts` (+0 -740) 📝 `packages/better-auth/src/plugins/two-factor/types.ts` (+73 -1) 📝 `packages/better-auth/src/plugins/two-factor/verify-two-factor.ts` (+59 -12) </details> ### 📄 Description - Added trust device functionality allowing users to mark devices as trusted, skipping 2FA on subsequent logins. - Introduced configuration options for trust device, including cookie name and max age. - Enhanced two-factor state management with options for storage strategy (cookie, database, or both) and state validity duration. - Updated documentation to reflect new features and usage examples. - Removed outdated test file for two-factor plugin. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Adds “trust this device” to 2FA and configurable 2FA state storage, reducing repeat prompts on known devices and giving apps control over where state lives (cookie, database, or both). - **New Features** - Trust device: optional cookie with configurable name and maxAge; skips 2FA on subsequent logins from that device. - Two-factor state options: choose storeStrategy (cookie | database | cookieAndDatabase), cookieName, and maxAge. - verifyTwoFactor now supports options (cookie name, trust device settings, store strategy, verification token). - TOTP and backup-code modules accept an injected verifyTwoFactor for better composition and testing. - Docs updated with examples and configuration. - **Migration** - Update OTP send handler signature: - Before: sendOTP({ user, otp }, request) - Now: sendOTP({ user, otp }, ctx) where ctx.query.otpDeliveryMethod is available. <sup>Written for commit d8683756e26599fa67a30cda834d0c48d1b83774. Summary will update automatically 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-17 23:23:21 -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#32624