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

Closed
opened 2026-04-13 09:28:26 -05:00 by GiteaMirror · 0 comments
Owner

Original Pull Request: https://github.com/better-auth/better-auth/pull/5735

State: closed
Merged: Yes


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.

**Original Pull Request:** https://github.com/better-auth/better-auth/pull/5735 **State:** closed **Merged:** Yes --- 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. -->
GiteaMirror added the pull-request label 2026-04-13 09:28:26 -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#14435