[PR #3318] [MERGED] fix(two-factor): trust device token refresh #30355

Closed
opened 2026-04-17 21:28:25 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/3318
Author: @gregtjack
Created: 7/9/2025
Status: Merged
Merged: 11/9/2025
Merged by: @Bekacru

Base: canaryHead: fix/2fa-trust-device-token-refresh


📝 Commits (10+)

  • c135c56 fix: 2fa token refresh
  • 7351f8b docs: trust device period
  • 76bed41 fix: properly refresh trust device cookie maxAge
  • 2a6a7b2 fix: cookie name
  • 4e57fb7 feat: add tests to ensure trust cookie is a signed token of that user
  • 7318012 Merge branch 'canary' into fix/2fa-trust-device-token-refresh
  • 8102658 Merge branch 'canary' into fix/2fa-trust-device-token-refresh
  • fbe4683 Merge branch 'canary' into fix/2fa-trust-device-token-refresh
  • 8cec351 properly refresh trust device cookie maxAge
  • 8d77067 add newline

📊 Changes

5 files changed (+68 additions, -10 deletions)

View changed files

📝 docs/content/docs/plugins/2fa.mdx (+1 -1)
📝 packages/better-auth/src/plugins/two-factor/constant.ts (+1 -0)
📝 packages/better-auth/src/plugins/two-factor/index.ts (+26 -7)
📝 packages/better-auth/src/plugins/two-factor/two-factor.test.ts (+34 -0)
📝 packages/better-auth/src/plugins/two-factor/verify-two-factor.ts (+6 -2)

📄 Description

  • Fixes an issue where after one successful login without triggering 2FA re-verification, on the next login users who set trustDevice are required to verify with 2FA, even if they are within the trust device 30 day period. This was caused by the refreshed trust_device token getting signed with the previous sessionToken instead of the new session token that's stored in the cookie. On the next login attempt the saved token and expected token didn't match, triggering 2FA.
  • Fixes an issue where maxAge of the refreshed cookie wasn't being set
  • Updates the docs to reflect the maxAge of the trust device cookie

Closes #2728.


🔄 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/3318 **Author:** [@gregtjack](https://github.com/gregtjack) **Created:** 7/9/2025 **Status:** ✅ Merged **Merged:** 11/9/2025 **Merged by:** [@Bekacru](https://github.com/Bekacru) **Base:** `canary` ← **Head:** `fix/2fa-trust-device-token-refresh` --- ### 📝 Commits (10+) - [`c135c56`](https://github.com/better-auth/better-auth/commit/c135c560ab09ee4c9140626ea6852e8d4d76be7b) fix: 2fa token refresh - [`7351f8b`](https://github.com/better-auth/better-auth/commit/7351f8b0d3915451719ccf2ec4a0b27d349c6719) docs: trust device period - [`76bed41`](https://github.com/better-auth/better-auth/commit/76bed41f3a11bda507caaff613e5957cb2db3e88) fix: properly refresh trust device cookie maxAge - [`2a6a7b2`](https://github.com/better-auth/better-auth/commit/2a6a7b239af2358b29dc3515b4a3cb931cc0e861) fix: cookie name - [`4e57fb7`](https://github.com/better-auth/better-auth/commit/4e57fb701cad5116237354971d39ca0b669ba495) feat: add tests to ensure trust cookie is a signed token of that user - [`7318012`](https://github.com/better-auth/better-auth/commit/7318012615a70c00e61ab61bf8312610b9dbc548) Merge branch 'canary' into fix/2fa-trust-device-token-refresh - [`8102658`](https://github.com/better-auth/better-auth/commit/8102658c024b19529d36bcfe1200b57301e3bf3b) Merge branch 'canary' into fix/2fa-trust-device-token-refresh - [`fbe4683`](https://github.com/better-auth/better-auth/commit/fbe468301a605981b0ca1f2efd7d1517ccdeffbe) Merge branch 'canary' into fix/2fa-trust-device-token-refresh - [`8cec351`](https://github.com/better-auth/better-auth/commit/8cec35112aee503032c684ed72cee02bf2c17fab) properly refresh trust device cookie maxAge - [`8d77067`](https://github.com/better-auth/better-auth/commit/8d770674e52f61134f73db780999c07cb3e86e23) add newline ### 📊 Changes **5 files changed** (+68 additions, -10 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/plugins/2fa.mdx` (+1 -1) 📝 `packages/better-auth/src/plugins/two-factor/constant.ts` (+1 -0) 📝 `packages/better-auth/src/plugins/two-factor/index.ts` (+26 -7) 📝 `packages/better-auth/src/plugins/two-factor/two-factor.test.ts` (+34 -0) 📝 `packages/better-auth/src/plugins/two-factor/verify-two-factor.ts` (+6 -2) </details> ### 📄 Description - Fixes an issue where after one successful login without triggering 2FA re-verification, on the next login users who set `trustDevice` are required to verify with 2FA, even if they are within the trust device 30 day period. This was caused by the refreshed trust_device token getting signed with the previous `sessionToken` instead of the new session token that's stored in the cookie. On the next login attempt the saved token and expected token didn't match, triggering 2FA. - Fixes an issue where `maxAge` of the refreshed cookie wasn't being set - Updates the docs to reflect the `maxAge` of the trust device cookie Closes #2728. --- <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 21:28:25 -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#30355