[PR #6452] [MERGED] fix: preserve user ID in cookie cache during stateless sessions #6674

Closed
opened 2026-03-13 13:07:41 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/6452
Author: @GautamBytes
Created: 12/1/2025
Status: Merged
Merged: 12/2/2025
Merged by: @Bekacru

Base: canaryHead: fix/stateless-id-persistence


📝 Commits (3)

  • 28ec77b fix(cookies): preserve user ID in cookie cache when generateId is false
  • 5c0cbcc remove typo
  • 7062dcf fix(schema): always preserve user id in output

📊 Changes

2 files changed (+34 additions, -1 deletions)

View changed files

📝 packages/better-auth/src/cookies/cookies.test.ts (+30 -0)
📝 packages/better-auth/src/db/schema.ts (+4 -1)

📄 Description

Fixes #6447

This PR fixes a bug where the user.id was being stripped from the session cookie (JWT) when running in stateless mode with generateId: false.

The Issue: In setCookieCache, the parseUserOutput helper filters the user object based on the configuration. When generateId is set to false (e.g., using an external provider's ID like Entra ID), the internal schema definition does not recognize id as a generated field, causing it to be filtered out. This resulted in 401 Unauthorized errors on API endpoints because the session was restored without a User ID.

The Fix: Modified setCookieCache in packages/better-auth/src/cookies/index.ts to explicitly merge session.user.id back into the filtered user object before tokenization.

Changes:

  • packages/better-auth/src/cookies/index.ts: Explicitly preserve id during cookie creation.
  • packages/better-auth/src/cookies/cookies.test.ts: Added a regression test to ensure user.id is always present in the cookie cache.

Summary by cubic

Fixes a bug where user.id was stripped from the session cookie in stateless mode (generateId: false), causing 401 errors when restoring sessions. The cookie cache now always preserves user.id.

  • Bug Fixes
    • Always preserve user.id in parseUserOutput so it isn't filtered out before cookie creation.
    • Add a regression test to ensure user.id is present in the cookie cache.

Written for commit 7062dcfcea. 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/6452 **Author:** [@GautamBytes](https://github.com/GautamBytes) **Created:** 12/1/2025 **Status:** ✅ Merged **Merged:** 12/2/2025 **Merged by:** [@Bekacru](https://github.com/Bekacru) **Base:** `canary` ← **Head:** `fix/stateless-id-persistence` --- ### 📝 Commits (3) - [`28ec77b`](https://github.com/better-auth/better-auth/commit/28ec77be839e8b6e50c22f461f8764e541d7a411) fix(cookies): preserve user ID in cookie cache when generateId is false - [`5c0cbcc`](https://github.com/better-auth/better-auth/commit/5c0cbcc20f7cde1a13585002696daf4f6c3b79f1) remove typo - [`7062dcf`](https://github.com/better-auth/better-auth/commit/7062dcfcead746e96565f31017d8ead03e80c1bf) fix(schema): always preserve user id in output ### 📊 Changes **2 files changed** (+34 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/cookies/cookies.test.ts` (+30 -0) 📝 `packages/better-auth/src/db/schema.ts` (+4 -1) </details> ### 📄 Description Fixes #6447 This PR fixes a bug where the user.id was being stripped from the session cookie (JWT) when running in stateless mode with generateId: false. **The Issue:** In setCookieCache, the parseUserOutput helper filters the user object based on the configuration. When generateId is set to false (e.g., using an external provider's ID like Entra ID), the internal schema definition does not recognize id as a generated field, causing it to be filtered out. This resulted in 401 Unauthorized errors on API endpoints because the session was restored without a User ID. **The Fix:** Modified setCookieCache in packages/better-auth/src/cookies/index.ts to explicitly merge session.user.id back into the filtered user object before tokenization. **Changes:** - packages/better-auth/src/cookies/index.ts: Explicitly preserve id during cookie creation. - packages/better-auth/src/cookies/cookies.test.ts: Added a regression test to ensure user.id is always present in the cookie cache. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fixes a bug where user.id was stripped from the session cookie in stateless mode (generateId: false), causing 401 errors when restoring sessions. The cookie cache now always preserves user.id. - **Bug Fixes** - Always preserve user.id in parseUserOutput so it isn't filtered out before cookie creation. - Add a regression test to ensure user.id is present in the cookie cache. <sup>Written for commit 7062dcfcead746e96565f31017d8ead03e80c1bf. 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 13:07:41 -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#6674