[GH-ISSUE #3713] customSession with cookieCache enabled not returning Set-Cookie header for session_token #9694

Closed
opened 2026-04-13 05:20:23 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @itsrichieryan on GitHub (Jul 31, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/3713

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Setup auth with customSession plugin and cookieCache enabled
  2. Perform getSession from client side
  3. Check the response headers

Current vs. Expected behavior

Current:
Set-Cookie header is returned for the session_data, but NOT for session_token

Expected:
Set-Cookie header is returned for both session_data and session_token

What version of Better Auth are you using?

1.3.4

Provide environment information

- OS: Ubuntu
- Browser: Chrome

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

Client

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  session: {
    expiresIn: 120,
    updateAge: 20, // Short update age to test session refresh
    cookieCache: {
      enabled: true,
      maxAge: 10,
    },
  },
  plugins: [
    customSession(async ({ user, session }) => {
      return {
        user: {
          ...user,
          newField: 'newField',
        },
        session,
      };
    }),
  ],
});

Additional context

The session_token cookie gets returned when customSession is NOT used.
The session_token cookie also gets returned if cookieCache is disabled, while customSession is being used.

This issue only happens when both customSession and cookieCache are being used together.

Given that the session_token Set-Cookie header is never returned on getSession, the expiry will not get refreshed and client will eventually be logged out, despite the session expiry getting refreshed on db side.

Could be related to https://github.com/better-auth/better-auth/issues/2106

Originally created by @itsrichieryan on GitHub (Jul 31, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/3713 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. Setup auth with customSession plugin and cookieCache enabled 2. Perform getSession from client side 3. Check the response headers ### Current vs. Expected behavior Current: Set-Cookie header is returned for the session_data, but NOT for session_token Expected: Set-Cookie header is returned for both session_data and session_token ### What version of Better Auth are you using? 1.3.4 ### Provide environment information ```bash - OS: Ubuntu - Browser: Chrome ``` ### Which area(s) are affected? (Select all that apply) Client ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ session: { expiresIn: 120, updateAge: 20, // Short update age to test session refresh cookieCache: { enabled: true, maxAge: 10, }, }, plugins: [ customSession(async ({ user, session }) => { return { user: { ...user, newField: 'newField', }, session, }; }), ], }); ``` ### Additional context The session_token cookie gets returned when customSession is NOT used. The session_token cookie also gets returned if cookieCache is disabled, while customSession is being used. This issue only happens when both customSession and cookieCache are being used together. Given that the session_token Set-Cookie header is never returned on getSession, the expiry will not get refreshed and client will eventually be logged out, despite the session expiry getting refreshed on db side. Could be related to https://github.com/better-auth/better-auth/issues/2106
GiteaMirror added the locked label 2026-04-13 05:20:24 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Jul 31, 2025):

Hi @itsrichieryan! I'm Dosu and I’m helping the better-auth team.

This is a known issue in Better Auth v1.3.4: when both customSession and cookieCache are enabled, if the session_data cookie is valid, the getSession endpoint returns the session from the cookie without updating or sending a Set-Cookie header for session_token. This means the session_token cookie is not refreshed, which can cause the client to be logged out even though the session is refreshed in the database. This behavior is confirmed in the code and has been discussed and addressed in recent updates to Better Auth (source).

As a workaround on v1.3.4, you can use the disableCookieCache query parameter on your getSession call to force a database fetch and trigger the session_token Set-Cookie header. Alternatively, disabling cookieCache or upgrading to a newer version of Better Auth (which includes fixes for this area) will resolve the issue (source).

To reply, just mention @dosu.


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

<!-- gh-comment-id:3139624774 --> @dosubot[bot] commented on GitHub (Jul 31, 2025): <!-- Greeting --> Hi @itsrichieryan! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> This is a known issue in Better Auth v1.3.4: when both customSession and cookieCache are enabled, if the session_data cookie is valid, the getSession endpoint returns the session from the cookie without updating or sending a Set-Cookie header for session_token. This means the session_token cookie is not refreshed, which can cause the client to be logged out even though the session is refreshed in the database. This behavior is confirmed in the code and has been discussed and addressed in recent updates to Better Auth ([source](https://github.com/better-auth/better-auth/blob/33b0635c4dc1a28ce217b4eadecc4ed24092f12e/packages/better-auth/src/api/routes/session.ts)). As a workaround on v1.3.4, you can use the disableCookieCache query parameter on your getSession call to force a database fetch and trigger the session_token Set-Cookie header. Alternatively, disabling cookieCache or upgrading to a newer version of Better Auth (which includes fixes for this area) will resolve the issue ([source](https://github.com/better-auth/better-auth/pull/3156)). <!-- 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/79316922-15a3-4e65-829a-9e5428052c96?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/79316922-15a3-4e65-829a-9e5428052c96?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/79316922-15a3-4e65-829a-9e5428052c96?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/79316922-15a3-4e65-829a-9e5428052c96?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/79316922-15a3-4e65-829a-9e5428052c96?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/79316922-15a3-4e65-829a-9e5428052c96?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/79316922-15a3-4e65-829a-9e5428052c96?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/3713)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#9694