[PR #5735] [MERGED] fix(session): persist additionalFields in cookie cache #23089

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

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/5735
Author: @Ridhim-RR
Created: 11/3/2025
Status: Merged
Merged: 11/4/2025
Merged by: @himself65

Base: canaryHead: feat/5700-additional-fields-cache


📝 Commits (4)

  • 78ca22b feat(session): persist additionalFields in cookie cache (fixes #5700)
  • fb1d478 chore: run biome format and lint fixes
  • 18bb38d refactor(dateFormat) : refreshedSession date format issue resolved
  • 0d5502e chore:(biome format) : run biome format and lint fixes

📊 Changes

2 files changed (+130 additions, -10 deletions)

View changed files

📝 packages/better-auth/src/api/routes/session-api.test.ts (+70 -0)
📝 packages/better-auth/src/api/routes/session.ts (+60 -10)

📄 Description

Fixes #5700

Fix: Additional fields declared in the session configuration should be persisted into the cache

Problem

When cookie cache is enabled, /api/auth/get-session doesn't include additionalFields declared in config. They come through as null or missing, forcing users to manually merge them via customSession plugin (which lacks proper TypeScript types).

Root Cause

  1. Cookie cache storage: Manual filtering excluded additionalFields when storing in cookies
  2. Cookie cache retrieval: Multiple code paths returned cached data without parsing additionalFields
  3. Database update path: updateSession returned raw data without parsing

Solution

  1. Fixed setCookieCache: Use parseSessionOutput instead of manual filtering (same as parseUserOutput)
  2. Fixed all cache retrieval paths: Parse cached data before returning to ensure additionalFields are included
  3. Fixed database update path: Parse updatedSession before returning

Changes

  • packages/better-auth/src/cookies/index.ts: Use parseSessionOutput for session data
  • packages/better-auth/src/api/routes/session.ts: Parse cached data in all return paths (cookie cache refresh disabled, refresh needed, default path, database update path)

Testing

Tested with cookie cache enabled - additionalFields now included in all scenarios:

  • Fresh cache
  • Cache refresh
  • Database update
  • dontRememberMe path

Fixes #5698


Summary by cubic

Persists session.additionalFields in the cookie cache and returns them from /api/auth/get-session across all paths. Addresses #5700 by fixing null/missing fields.

  • Bug Fixes
    • Use parseSessionOutput when writing to cookie cache to avoid filtering out additionalFields.
    • Parse session and user from cache/updates before returning, rehydrating date fields to ensure correct parsing (cache refresh, cache hit, dontRememberMe, DB update).
    • Add test to confirm additionalFields are present from cookie cache and values match.

Written for commit 0d5502e309. 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/5735 **Author:** [@Ridhim-RR](https://github.com/Ridhim-RR) **Created:** 11/3/2025 **Status:** ✅ Merged **Merged:** 11/4/2025 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `feat/5700-additional-fields-cache` --- ### 📝 Commits (4) - [`78ca22b`](https://github.com/better-auth/better-auth/commit/78ca22b5833d2bbd672ecb27387fb3f2f1098df6) feat(session): persist additionalFields in cookie cache (fixes #5700) - [`fb1d478`](https://github.com/better-auth/better-auth/commit/fb1d478f127e9424e4f721c56db9240331a679c6) chore: run biome format and lint fixes - [`18bb38d`](https://github.com/better-auth/better-auth/commit/18bb38d2976d594329e927f4cf03f50a2764b323) refactor(dateFormat) : refreshedSession date format issue resolved - [`0d5502e`](https://github.com/better-auth/better-auth/commit/0d5502e309c78bc2b7882f687194cf126dc58bcc) chore:(biome format) : run biome format and lint fixes ### 📊 Changes **2 files changed** (+130 additions, -10 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/api/routes/session-api.test.ts` (+70 -0) 📝 `packages/better-auth/src/api/routes/session.ts` (+60 -10) </details> ### 📄 Description Fixes #5700 # Fix: Additional fields declared in the session configuration should be persisted into the cache ## Problem When cookie cache is enabled, `/api/auth/get-session` doesn't include `additionalFields` declared in config. They come through as `null` or missing, forcing users to manually merge them via `customSession` plugin (which lacks proper TypeScript types). ## Root Cause 1. **Cookie cache storage**: Manual filtering excluded `additionalFields` when storing in cookies 2. **Cookie cache retrieval**: Multiple code paths returned cached data without parsing `additionalFields` 3. **Database update path**: `updateSession` returned raw data without parsing ## Solution 1. **Fixed `setCookieCache`**: Use `parseSessionOutput` instead of manual filtering (same as `parseUserOutput`) 2. **Fixed all cache retrieval paths**: Parse cached data before returning to ensure `additionalFields` are included 3. **Fixed database update path**: Parse `updatedSession` before returning ## Changes - `packages/better-auth/src/cookies/index.ts`: Use `parseSessionOutput` for session data - `packages/better-auth/src/api/routes/session.ts`: Parse cached data in all return paths (cookie cache refresh disabled, refresh needed, default path, database update path) ## Testing Tested with cookie cache enabled - `additionalFields` now included in all scenarios: - Fresh cache - Cache refresh - Database update - `dontRememberMe` path Fixes #5698 <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Persists session.additionalFields in the cookie cache and returns them from /api/auth/get-session across all paths. Addresses #5700 by fixing null/missing fields. - **Bug Fixes** - Use parseSessionOutput when writing to cookie cache to avoid filtering out additionalFields. - Parse session and user from cache/updates before returning, rehydrating date fields to ensure correct parsing (cache refresh, cache hit, dontRememberMe, DB update). - Add test to confirm additionalFields are present from cookie cache and values match. <sup>Written for commit 0d5502e309c78bc2b7882f687194cf126dc58bcc. 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-15 21:28: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#23089