[PR #6031] [MERGED] fix: resolve SESSION_IS_NOT_FRESH error with cookieCache #6396

Closed
opened 2026-03-13 12:57:25 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/6031
Author: @GautamBytes
Created: 11/17/2025
Status: Merged
Merged: 11/17/2025
Merged by: @Bekacru

Base: canaryHead: fix/session-freshness-cookie-cache


📝 Commits (1)

  • e3498d7 Fix session freshness check for cookie cache

📊 Changes

1 file changed (+3 additions, -2 deletions)

View changed files

📝 packages/better-auth/src/api/routes/session.ts (+3 -2)

📄 Description

Closes #6028

Description

Fixes a bug where freshSessionMiddleware incorrectly threw SESSION_IS_NOT_FRESH when cookieCache was enabled.

The issue was caused by the updatedAt field being retrieved from the cookie as an ISO string, while the freshness logic used .valueOf() directly, which failed to perform the correct math against Date.now().

The Fix

Updated freshSessionMiddleware in session.ts to explicitly wrap updatedAt (or createdAt) in new Date(...).getTime(). This ensures a numeric timestamp is always used for comparison, regardless of whether the input is a Date object or an ISO string.


Summary by cubic

Fixes the session freshness check to stop false SESSION_IS_NOT_FRESH errors when cookieCache is enabled. Normalizes cookie dates to numeric timestamps before comparison.

  • Bug Fixes
    • In freshSessionMiddleware, compute lastUpdated with new Date(updatedAt || createdAt).getTime() so ISO strings from cookies compare correctly against Date.now().

Written for commit e3498d79bb. 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/6031 **Author:** [@GautamBytes](https://github.com/GautamBytes) **Created:** 11/17/2025 **Status:** ✅ Merged **Merged:** 11/17/2025 **Merged by:** [@Bekacru](https://github.com/Bekacru) **Base:** `canary` ← **Head:** `fix/session-freshness-cookie-cache` --- ### 📝 Commits (1) - [`e3498d7`](https://github.com/better-auth/better-auth/commit/e3498d79bb4920e43788107c62e0507cda16c799) Fix session freshness check for cookie cache ### 📊 Changes **1 file changed** (+3 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/api/routes/session.ts` (+3 -2) </details> ### 📄 Description Closes #6028 ### Description Fixes a bug where `freshSessionMiddleware` incorrectly threw `SESSION_IS_NOT_FRESH` when `cookieCache` was enabled. The issue was caused by the `updatedAt` field being retrieved from the cookie as an ISO string, while the freshness logic used `.valueOf()` directly, which failed to perform the correct math against `Date.now()`. ### The Fix Updated `freshSessionMiddleware` in `session.ts` to explicitly wrap `updatedAt` (or `createdAt`) in `new Date(...).getTime()`. This ensures a numeric timestamp is always used for comparison, regardless of whether the input is a Date object or an ISO string. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fixes the session freshness check to stop false SESSION_IS_NOT_FRESH errors when cookieCache is enabled. Normalizes cookie dates to numeric timestamps before comparison. - **Bug Fixes** - In freshSessionMiddleware, compute lastUpdated with new Date(updatedAt || createdAt).getTime() so ISO strings from cookies compare correctly against Date.now(). <sup>Written for commit e3498d79bb4920e43788107c62e0507cda16c799. 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:57:25 -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#6396