SESSION_IS_NOT_FRESH thrown, when cookieCache is enabled #2319

Closed
opened 2026-03-13 09:43:50 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @proof-llc on GitHub (Nov 16, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Enable cookieCache
  2. Go to webpage /api/auth/passkey/generate-register-options?authenticatorAttachment=platform
  3. You'll get SESSION_IS_NOT_FRESH
  4. Wait beyond CookieCache.maxAge (In my case, 5 minutes is enough)
  5. Refresh page and you'll get proper results - not SESSION_IS_NOT_FRESH

Current vs. Expected behavior

I expect to not have freshness error right after logging in especially when default freshness is set to 1 day.

when cookieCache is enabled, updatedAt is in the wrong format (2025-11-16T18:07:42.283Z instead of 1763327105228) which results in a bad comparison which results in error.

What version of Better Auth are you using?

1.4.0 beta

System info

{
  "system": {
    "platform": "darwin",
    "arch": "arm64",
    "version": "Darwin Kernel Version 24.6.0: Mon Jul 14 11:30:40 PDT 2025;",
    "release": "24.6.0",
    "cpuCount": 16,
    "cpuModel": "redacted",
    "totalMemory": "128.00 GB",
    "freeMemory": "20.62 GB"
  },
  "node": {
    "version": "v24.7.0",
    "env": "development"
  },
  "packageManager": {
    "name": "npm",
    "version": "11.5.1"
  },
  "frameworks": null,
  "databases": null,
  "betterAuth": {
    "version": "0.1.0",
    "config": null
  }
}

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
  cookieCache: {
        enabled: true,
        maxAge: 5 * 60, // 5 min
        strategy: 'jwe',
  }
});

Additional context

https://github.com/better-auth/better-auth/blob/canary/packages/better-auth/src/api/routes/session.ts#L548

Originally created by @proof-llc on GitHub (Nov 16, 2025). ### Is this suited for github? - [ ] Yes, this is suited for github ### To Reproduce 1. Enable cookieCache 2. Go to webpage /api/auth/passkey/generate-register-options?authenticatorAttachment=platform 3. You'll get SESSION_IS_NOT_FRESH 4. Wait beyond CookieCache.maxAge (In my case, 5 minutes is enough) 5. Refresh page and you'll get proper results - not SESSION_IS_NOT_FRESH ### Current vs. Expected behavior I expect to not have freshness error right after logging in especially when default freshness is set to 1 day. when cookieCache is enabled, updatedAt is in the wrong format (2025-11-16T18:07:42.283Z instead of 1763327105228) which results in a bad comparison which results in error. ### What version of Better Auth are you using? 1.4.0 beta ### System info ```bash { "system": { "platform": "darwin", "arch": "arm64", "version": "Darwin Kernel Version 24.6.0: Mon Jul 14 11:30:40 PDT 2025;", "release": "24.6.0", "cpuCount": 16, "cpuModel": "redacted", "totalMemory": "128.00 GB", "freeMemory": "20.62 GB" }, "node": { "version": "v24.7.0", "env": "development" }, "packageManager": { "name": "npm", "version": "11.5.1" }, "frameworks": null, "databases": null, "betterAuth": { "version": "0.1.0", "config": null } } ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, cookieCache: { enabled: true, maxAge: 5 * 60, // 5 min strategy: 'jwe', } }); ``` ### Additional context https://github.com/better-auth/better-auth/blob/canary/packages/better-auth/src/api/routes/session.ts#L548
GiteaMirror added the bug label 2026-03-13 09:43:50 -05:00
Author
Owner

@better-auth-agent[bot] commented on GitHub (Nov 16, 2025):

(tag @better-auth-agent if you would like an answer from the Agent)

DiagramDiscordGitHub

Diagram Join Star

@better-auth-agent[bot] commented on GitHub (Nov 16, 2025): (tag @better-auth-agent if you would like an answer from the Agent) <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) • [Discord](https://discord.gg/better-auth) • [GitHub](https://github.com/better-auth/better-auth) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/better-auth) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/better-auth/better-auth)
Author
Owner

@dosubot[bot] commented on GitHub (Nov 16, 2025):

Yes, this is a valid GitHub issue and is already known in the community. The root cause is that when cookieCache is enabled, the updatedAt field is serialized as an ISO string (e.g., "2025-11-16T18:07:42.283Z") in the cookie, but the session freshness check expects a numeric timestamp (milliseconds since epoch). This mismatch causes the SESSION_IS_NOT_FRESH error even right after login, regardless of your freshness settings. The relevant code compares updatedAt using valueOf(), which works for Date objects or timestamps, but fails for ISO strings, leading to the error you’re seeing (source).

A fix would involve ensuring updatedAt is always stored and serialized as a numeric timestamp (milliseconds) in both the database and the cookie cache. Until this is fixed upstream, a workaround is to disable cookieCache or manually patch the serialization logic to convert updatedAt to a number before storing it in the cookie (discussion).

This issue is confirmed in multiple versions and is not specific to your setup. For now, disabling cookieCache avoids the bug, but a code fix is needed for full compatibility.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

@dosubot[bot] commented on GitHub (Nov 16, 2025): <!-- Answer --> Yes, this is a valid GitHub issue and is already known in the community. The root cause is that when cookieCache is enabled, the updatedAt field is serialized as an ISO string (e.g., "2025-11-16T18:07:42.283Z") in the cookie, but the session freshness check expects a numeric timestamp (milliseconds since epoch). This mismatch causes the SESSION_IS_NOT_FRESH error even right after login, regardless of your freshness settings. The relevant code compares updatedAt using valueOf(), which works for Date objects or timestamps, but fails for ISO strings, leading to the error you’re seeing ([source](https://github.com/better-auth/better-auth/blob/d768852258d1fea921984196806608af2983a867/packages/better-auth/src/api/routes/session.ts#L91-L364)). A fix would involve ensuring updatedAt is always stored and serialized as a numeric timestamp (milliseconds) in both the database and the cookie cache. Until this is fixed upstream, a workaround is to disable cookieCache or manually patch the serialization logic to convert updatedAt to a number before storing it in the cookie ([discussion](https://github.com/better-auth/better-auth/issues/1297)). This issue is confirmed in multiple versions and is not specific to your setup. For now, disabling cookieCache avoids the bug, but a code fix is needed for full compatibility. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/e0888f7a-b776-46e5-a651-4f0c903f31fa?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/e0888f7a-b776-46e5-a651-4f0c903f31fa?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/e0888f7a-b776-46e5-a651-4f0c903f31fa?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/e0888f7a-b776-46e5-a651-4f0c903f31fa?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/e0888f7a-b776-46e5-a651-4f0c903f31fa?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/e0888f7a-b776-46e5-a651-4f0c903f31fa?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/e0888f7a-b776-46e5-a651-4f0c903f31fa?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/6028)
Author
Owner

@GautamBytes commented on GitHub (Nov 17, 2025):

working on this!!

@GautamBytes commented on GitHub (Nov 17, 2025): working on this!!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#2319