[PR #8817] fix(session): preserve real session expiry during stateless cache refresh #16480

Open
opened 2026-04-13 10:32:16 -05:00 by GiteaMirror · 0 comments
Owner

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

State: open
Merged: No


Description

Fixes #8770

When cookieCache.refreshCache is enabled (stateless mode), the get-session endpoint was incorrectly overwriting session.session.expiresAt with now + cookieCache.maxAge instead of preserving the real session expiry.

Root Cause

In the stateless cache refresh path, refreshedSession was overriding expiresAt with the cookie cache TTL:

const newExpiresAt = getDate(cookieMaxAge, "sec");
const refreshedSession = {
  session: {
    ...session.session,
    expiresAt: newExpiresAt, // ← overwrites real session expiry with cache TTL
  },
  ...
};

Fix

Remove the expiresAt: newExpiresAt override. The spread ...session.session already carries the real expiresAt, and setCookieCache sets the JWE cookie expiration independently from authCookies.sessionData.attributes.maxAge.

Changes

  • Removed newExpiresAt variable
  • Removed expiresAt: newExpiresAt from refreshedSession

Summary by cubic

Preserves the real session expiration when cookieCache.refreshCache is enabled by removing the incorrect expiresAt override in the get-session refresh path. Prevents long‑lived sessions from being shortened to the cookie cache TTL; fixes #8770.

Written for commit e8da9e5aad. Summary will update on new commits.

**Original Pull Request:** https://github.com/better-auth/better-auth/pull/8817 **State:** open **Merged:** No --- ## Description Fixes #8770 When `cookieCache.refreshCache` is enabled (stateless mode), the get-session endpoint was incorrectly overwriting `session.session.expiresAt` with `now + cookieCache.maxAge` instead of preserving the real session expiry. ### Root Cause In the stateless cache refresh path, `refreshedSession` was overriding `expiresAt` with the cookie cache TTL: ```typescript const newExpiresAt = getDate(cookieMaxAge, "sec"); const refreshedSession = { session: { ...session.session, expiresAt: newExpiresAt, // ← overwrites real session expiry with cache TTL }, ... }; ``` ### Fix Remove the `expiresAt: newExpiresAt` override. The spread `...session.session` already carries the real `expiresAt`, and `setCookieCache` sets the JWE cookie expiration independently from `authCookies.sessionData.attributes.maxAge`. ### Changes - Removed `newExpiresAt` variable - Removed `expiresAt: newExpiresAt` from `refreshedSession` <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Preserves the real session expiration when `cookieCache.refreshCache` is enabled by removing the incorrect `expiresAt` override in the get-session refresh path. Prevents long‑lived sessions from being shortened to the cookie cache TTL; fixes #8770. <sup>Written for commit e8da9e5aadaec89a36e3d8d14417292d1d829bac. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. -->
GiteaMirror added the pull-request label 2026-04-13 10:32:16 -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#16480