[PR #4505] [MERGED] fix(multi-session): multi-session cookie name preface preventing multiple accounts signed in #22316

Closed
opened 2026-04-15 20:57:39 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/4505
Author: @PacifismPostMortem
Created: 9/8/2025
Status: Merged
Merged: 9/9/2025
Merged by: @himself65

Base: canaryHead: canary


📝 Commits (2)

  • 211e971 fix(multi-session): Fix multi-session cookie name preface
  • a0e7fc1 Merge branch 'canary' into canary

📊 Changes

1 file changed (+8 additions, -4 deletions)

View changed files

📝 packages/better-auth/src/plugins/multi-session/index.ts (+8 -4)

📄 Description

Issue / Context

When logging into and out of multiple accounts using the multi-session plugin, the maximum number of accounts seems to be sporadic. I would only be able to log into 1 account sometimes, other times 2 or 3 accounts were possible. This is only a problem in production, not dev localhost.

Investigating the cookies and network requests, I found the multi-session cookies are not being cleared upon logging out.
The sessions are properly deleted in the database, but the multi-session cookies aren't cleared in the browser, despite the get-session request seemingly having headers that cleared those cookies.

This results in the maximum number of accounts being reached despite not all the cookies being valid. The sporadic behavior is from different patterns of logging in and out (I was clearing/deleting cookies during testing, so it was dependent on how many invalid multi-session cookies existed).

Source & Fix

The sign-out response multi-session cookie names are prefaced with "__secure-" instead of "__Secure-", so the cookies aren't cleared (which is why this bug only exists in production / all environments with https).
image
Cookies after signing out:
image

In the multi-session plugin, the sign out handler uses an all lowercase cookie name, when the preface must have casing "__Secure-". I changed this line to replace "__secure-" with "__Secure-".

This properly sets and clears cookies so multiple accounts can be added (aside from the off by one error found in #4490).


Summary by cubic

Fixes multi-session logout not clearing cookies in production by using the correct "__Secure-" prefix. This resolves sporadic “max accounts reached” errors and restores reliable multi-account sign-in.

  • Bug Fixes
    • Sign-out now clears multi-session cookies by setting names with "__Secure-" instead of "__secure-".
    • Prevents stale cookies in HTTPS environments that blocked new sessions.

🔄 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/4505 **Author:** [@PacifismPostMortem](https://github.com/PacifismPostMortem) **Created:** 9/8/2025 **Status:** ✅ Merged **Merged:** 9/9/2025 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `canary` --- ### 📝 Commits (2) - [`211e971`](https://github.com/better-auth/better-auth/commit/211e97187a65f086d6a595d5db935fbe68ee8c31) fix(multi-session): Fix multi-session cookie name preface - [`a0e7fc1`](https://github.com/better-auth/better-auth/commit/a0e7fc1150ed3f35c3d320ddd557436e947973e3) Merge branch 'canary' into canary ### 📊 Changes **1 file changed** (+8 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/plugins/multi-session/index.ts` (+8 -4) </details> ### 📄 Description # Issue / Context When logging into and out of multiple accounts using the multi-session plugin, the maximum number of accounts seems to be sporadic. I would only be able to log into 1 account sometimes, other times 2 or 3 accounts were possible. This is only a problem in production, not dev localhost. Investigating the cookies and network requests, I found the multi-session cookies are not being cleared upon logging out. The sessions are properly deleted in the database, but the multi-session cookies aren't cleared in the browser, despite the get-session request seemingly having headers that cleared those cookies. This results in the [maximum number of accounts](https://github.com/PacifismPostMortem/better-auth/blob/12daa395414a7f0663abf1cb9bbd001fe1e0d682/packages/better-auth/src/plugins/multi-session/index.ts#L306) being reached despite not all the cookies being valid. The sporadic behavior is from different patterns of logging in and out (I was clearing/deleting cookies during testing, so it was dependent on how many invalid multi-session cookies existed). # Source & Fix The sign-out response multi-session cookie names are prefaced with "__secure-" instead of "__Secure-", so the cookies aren't cleared (which is why this bug only exists in production / all environments with https). <img width="1820" height="909" alt="image" src="https://github.com/user-attachments/assets/3eb77f28-37c8-4ca2-8064-7a68b00cc0ed" /> Cookies after signing out: <img width="330" height="170" alt="image" src="https://github.com/user-attachments/assets/d8b3ae84-4c8f-46be-9c2f-33648347e72c" /> In the multi-session plugin, the [sign out handler](https://github.com/better-auth/better-auth/blob/9829c4dea2536bdc9396b3ee9a8ae7d5273944f8/packages/better-auth/src/plugins/multi-session/index.ts#L332) uses an all lowercase cookie name, when the preface must have casing "__Secure-". I changed this line to replace "__secure-" with "__Secure-". This properly sets and clears cookies so multiple accounts can be added (aside from the off by one error found in #4490). <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fixes multi-session logout not clearing cookies in production by using the correct "__Secure-" prefix. This resolves sporadic “max accounts reached” errors and restores reliable multi-account sign-in. - **Bug Fixes** - Sign-out now clears multi-session cookies by setting names with "__Secure-" instead of "__secure-". - Prevents stale cookies in HTTPS environments that blocked new sessions. <!-- 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 20:57:39 -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#22316