[PR #6107] fix(twofactor): overwrite existing session cookies instead of deleting them (#6077) #6449

Open
opened 2026-03-13 12:59:44 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/6107
Author: @SanderVHGD
Created: 11/19/2025
Status: 🔄 Open

Base: canaryHead: fix/two-factor-overwrite-session-cookies


📝 Commits (1)

  • 7c82c60 fix(twofactor): overwrite existing session cookies instead of deleting them (#6077)

📊 Changes

2 files changed (+0 additions, -5 deletions)

View changed files

📝 packages/better-auth/src/plugins/two-factor/index.ts (+0 -4)
📝 packages/better-auth/src/plugins/two-factor/totp/index.ts (+0 -1)

📄 Description

Potential fix for #6077.

We now overwrite the session cookie in disableTwoFactor and verifyTotp in the twofactor plugin instead of deleting the session cookie and afterwards setting a new one.
This is potential fix for how Next.js handles cookies. Now the page will refresh only once instead of twice when changing the cookies and getSession will not return null for the short period where there is no cookie set.


Summary by cubic

Overwrite the session cookie when disabling two-factor and verifying TOTP. This aligns with Next.js cookie handling and prevents double page refreshes and transient null sessions.

  • Bug Fixes
    • Removed internalAdapter.deleteSession calls; setSessionCookie now updates the existing cookie in place.
    • Applied in twoFactor and TOTP flows; fixes #6077.

Written for commit 7c82c603a8. 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/6107 **Author:** [@SanderVHGD](https://github.com/SanderVHGD) **Created:** 11/19/2025 **Status:** 🔄 Open **Base:** `canary` ← **Head:** `fix/two-factor-overwrite-session-cookies` --- ### 📝 Commits (1) - [`7c82c60`](https://github.com/better-auth/better-auth/commit/7c82c603a8f84ddf9f749e0fe846eab46f8f9680) fix(twofactor): overwrite existing session cookies instead of deleting them (#6077) ### 📊 Changes **2 files changed** (+0 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/plugins/two-factor/index.ts` (+0 -4) 📝 `packages/better-auth/src/plugins/two-factor/totp/index.ts` (+0 -1) </details> ### 📄 Description Potential fix for [#6077](https://github.com/better-auth/better-auth/issues/6077). We now overwrite the session cookie in `disableTwoFactor` and `verifyTotp` in the twofactor plugin instead of deleting the session cookie and afterwards setting a new one. This is potential fix for how Next.js handles cookies. Now the page will refresh only once instead of twice when changing the cookies and `getSession` will not return `null` for the short period where there is no cookie set. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Overwrite the session cookie when disabling two-factor and verifying TOTP. This aligns with Next.js cookie handling and prevents double page refreshes and transient null sessions. - **Bug Fixes** - Removed internalAdapter.deleteSession calls; setSessionCookie now updates the existing cookie in place. - Applied in twoFactor and TOTP flows; fixes #6077. <sup>Written for commit 7c82c603a8f84ddf9f749e0fe846eab46f8f9680. 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-03-13 12:59:44 -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#6449