auth.api.getSession always returns null if session.cookieCache is not set in the auth #2028

Closed
opened 2026-03-13 09:21:46 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @npbk on GitHub (Sep 27, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Log in using authClient.signIn.social successfully
  2. Navigate into dashboard route
  3. Calls await auth.api.getSession results null always, despite better-auth.session_token already set in cookie
  4. Add session.cookieCache.enabled = true to auth
  5. Now the auth.api.getSession works :)

Current vs. Expected behavior

Cannot use the auth.api.getSession without set the session.cookieCache.enabled to true, if this is mandatory then please help update the document

What version of Better Auth are you using?

System info

{
  "system": {
    "platform": "darwin",
    "arch": "arm64",
    "version": "Darwin Kernel Version 24.6.0: Mon Jul 14 11:28:30 PDT 2025; root:xnu-11417.140.69~1/RELEASE_ARM64_T6030",
    "release": "24.6.0",
    "cpuCount": 11,
    "cpuModel": "Apple M3 Pro",
    "totalMemory": "18.00 GB",
    "freeMemory": "0.25 GB"
  },
  "node": {
    "version": "v22.18.0",
    "env": "development"
  },
  "packageManager": {
    "name": "npm",
    "version": "10.9.3"
  },
  "frameworks": [
    {
      "name": "next",
      "version": "15.5.3"
    },
    {
      "name": "react",
      "version": "19.1.0"
    }
  ],
  "databases": null,
  "betterAuth": {
    "version": "^1.3.18",
    "config": null
  }
}

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

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth";
import { nextCookies } from "better-auth/next-js";

export const auth = betterAuth({
  socialProviders: {
    google: {
      clientId: process.env.GOOGLE_CLIENT_ID!,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
    },
  },
  plugins: [nextCookies()],
});

Additional context

No response

Originally created by @npbk on GitHub (Sep 27, 2025). ### Is this suited for github? - [ ] Yes, this is suited for github ### To Reproduce 1. Log in using authClient.signIn.social successfully 2. Navigate into dashboard route 3. Calls await auth.api.getSession results `null` always, despite better-auth.session_token already set in cookie 4. Add `session.cookieCache.enabled = true` to `auth` 5. Now the auth.api.getSession works :) ### Current vs. Expected behavior Cannot use the auth.api.getSession without set the session.cookieCache.enabled to true, if this is mandatory then please help update the document ### What version of Better Auth are you using? 1. ### System info ```bash { "system": { "platform": "darwin", "arch": "arm64", "version": "Darwin Kernel Version 24.6.0: Mon Jul 14 11:28:30 PDT 2025; root:xnu-11417.140.69~1/RELEASE_ARM64_T6030", "release": "24.6.0", "cpuCount": 11, "cpuModel": "Apple M3 Pro", "totalMemory": "18.00 GB", "freeMemory": "0.25 GB" }, "node": { "version": "v22.18.0", "env": "development" }, "packageManager": { "name": "npm", "version": "10.9.3" }, "frameworks": [ { "name": "next", "version": "15.5.3" }, { "name": "react", "version": "19.1.0" } ], "databases": null, "betterAuth": { "version": "^1.3.18", "config": null } } ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth"; import { nextCookies } from "better-auth/next-js"; export const auth = betterAuth({ socialProviders: { google: { clientId: process.env.GOOGLE_CLIENT_ID!, clientSecret: process.env.GOOGLE_CLIENT_SECRET!, }, }, plugins: [nextCookies()], }); ``` ### Additional context _No response_
GiteaMirror added the nextjs label 2026-03-13 09:21:46 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Sep 27, 2025):

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

Yes, this is a valid issue for GitHub. The behavior you described is expected based on the current implementation: if session.cookieCache.enabled is not set to true, auth.api.getSession will always return null, even if the session_token cookie is present. This is because the session data cookie (session_data) is only set and read when cookieCache is enabled, and getSession relies on this for session retrieval in many scenarios. This requirement is not currently documented, but it is a functional necessity, not just an optimization. It would be helpful for the documentation to clarify that enabling session.cookieCache is required for getSession to work as expected in these cases. For more technical details, see this code reference and related issue discussion.

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 (Sep 27, 2025): <!-- Greeting --> Hi @npbk! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> Yes, this is a valid issue for GitHub. The behavior you described is expected based on the current implementation: if session.cookieCache.enabled is not set to true, auth.api.getSession will always return null, even if the session_token cookie is present. This is because the session data cookie (session_data) is only set and read when cookieCache is enabled, and getSession relies on this for session retrieval in many scenarios. This requirement is not currently documented, but it is a functional necessity, not just an optimization. It would be helpful for the documentation to clarify that enabling session.cookieCache is required for getSession to work as expected in these cases. For more technical details, see [this code reference](https://github.com/better-auth/better-auth/blob/f6cbdcc84ee5d2971fdcc8b23ff7c174f88cf45b/packages/better-auth/src/cookies/index.ts) and related [issue discussion](https://github.com/better-auth/better-auth/issues/4517). <!-- 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/2daa5b19-9631-41fb-afc5-75716ceec713?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/2daa5b19-9631-41fb-afc5-75716ceec713?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/2daa5b19-9631-41fb-afc5-75716ceec713?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/2daa5b19-9631-41fb-afc5-75716ceec713?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/2daa5b19-9631-41fb-afc5-75716ceec713?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/2daa5b19-9631-41fb-afc5-75716ceec713?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/2daa5b19-9631-41fb-afc5-75716ceec713?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/4942)
Author
Owner

@Bekacru commented on GitHub (Sep 30, 2025):

what version are you using?

@Bekacru commented on GitHub (Sep 30, 2025): what version are you using?
Author
Owner

@npbk commented on GitHub (Sep 30, 2025):

1.3.18 @Bekacru

@npbk commented on GitHub (Sep 30, 2025): 1.3.18 @Bekacru
Author
Owner

@Bekacru commented on GitHub (Sep 30, 2025):

this is most likely due to some cookie mismatch. make sure to clear your cookies and retry again without enabling cookie cache it should work

@Bekacru commented on GitHub (Sep 30, 2025): this is most likely due to some cookie mismatch. make sure to clear your cookies and retry again without enabling cookie cache it should work
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#2028