secondaryStorage session not updated after organization creation #1896

Closed
opened 2026-03-13 09:11:23 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @ninthnightfall on GitHub (Sep 10, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

on server enable secondaryStorage like
secondaryStorage: cacheService
on client call
await authClient.organization.create({ name: '', slug: '' })
then call
await authClient.getSession({ query: { disableCookieCache: true } })
or
await authClient.organization.setActive({ organization: '' })
activeOrganizationId and activeTeamId in cache are not updated

when disable secondaryStorage and use cookieCache, it works well

Current vs. Expected behavior

Current
session in secondaryStorage not updated after organization creation
Expected
after the organization is created, the session in secondaryStorage should be updated

What version of Better Auth are you using?

1.3.8

System info

{
  "system": {
    "platform": "win32",
    "arch": "x64",
    "version": "Windows 11 Pro",
    "release": "10.0.22631",
    "cpuCount": 20,
    "cpuModel": "Intel(R) Core(TM) i7-14700K CPU @ 3.40GHz",
    "totalMemory": "31.88 GB",
    "freeMemory": "16.73 GB"
  },
  "node": {
    "version": "v20.19.4",
    "env": "development"
  },
  "packageManager": {
    "name": "npm",
    "version": "10.8.2"
  },
  "frameworks": null,
  "databases": [
    {
      "name": "pg",
      "version": "^8.16.3"
    },
    {
      "name": "drizzle",
      "version": "^0.44.5"
    }
  ],
  "betterAuth": {
    "version": "0.0.1",
    "config": null
  }
}

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

Backend

Auth config (if applicable)


Additional context

No response

Originally created by @ninthnightfall on GitHub (Sep 10, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce on server enable secondaryStorage like `secondaryStorage: cacheService` on client call `await authClient.organization.create({ name: '', slug: '' })` then call `await authClient.getSession({ query: { disableCookieCache: true } })` or `await authClient.organization.setActive({ organization: '' })` activeOrganizationId and activeTeamId in cache are not updated when disable secondaryStorage and use cookieCache, it works well ### Current vs. Expected behavior Current session in secondaryStorage not updated after organization creation Expected after the organization is created, the session in secondaryStorage should be updated ### What version of Better Auth are you using? 1.3.8 ### System info ```bash { "system": { "platform": "win32", "arch": "x64", "version": "Windows 11 Pro", "release": "10.0.22631", "cpuCount": 20, "cpuModel": "Intel(R) Core(TM) i7-14700K CPU @ 3.40GHz", "totalMemory": "31.88 GB", "freeMemory": "16.73 GB" }, "node": { "version": "v20.19.4", "env": "development" }, "packageManager": { "name": "npm", "version": "10.8.2" }, "frameworks": null, "databases": [ { "name": "pg", "version": "^8.16.3" }, { "name": "drizzle", "version": "^0.44.5" } ], "betterAuth": { "version": "0.0.1", "config": null } } ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript ``` ### Additional context _No response_
GiteaMirror added the bug label 2026-03-13 09:11:23 -05:00
Author
Owner

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

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

This is a known limitation: when using secondaryStorage, session updates after organization creation or switching the active organization may not be automatically reflected in the cache, unlike with cookieCache. The update logic for secondaryStorage isn't always triggered in these flows, which leads to the discrepancy you're seeing. The officially supported approach is to manually refresh the session after such changes (for example, by calling a refetch or getSession again after setActive) 1.

Recent changes to secondaryStorage improved data retrieval robustness but did not address automatic session updates after organization changes 2. If you need the session to reflect the new organization, manually set the active organization after creation and trigger a session refresh. This is currently expected behavior 3.

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 10, 2025): <!-- Greeting --> Hi @ninthnightfall! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> This is a known limitation: when using secondaryStorage, session updates after organization creation or switching the active organization may not be automatically reflected in the cache, unlike with cookieCache. The update logic for secondaryStorage isn't always triggered in these flows, which leads to the discrepancy you're seeing. The officially supported approach is to manually refresh the session after such changes (for example, by calling a refetch or getSession again after setActive) [1](https://github.com/better-auth/better-auth/issues/3837). Recent changes to secondaryStorage improved data retrieval robustness but did not address automatic session updates after organization changes [2](https://github.com/better-auth/better-auth/pull/4076). If you need the session to reflect the new organization, manually set the active organization after creation and trigger a session refresh. This is currently expected behavior [3](https://github.com/better-auth/better-auth/issues/3591). <!-- 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/ddca84a1-8eae-41db-beee-492f7c721ae1?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/ddca84a1-8eae-41db-beee-492f7c721ae1?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/ddca84a1-8eae-41db-beee-492f7c721ae1?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/ddca84a1-8eae-41db-beee-492f7c721ae1?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/ddca84a1-8eae-41db-beee-492f7c721ae1?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/ddca84a1-8eae-41db-beee-492f7c721ae1?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/ddca84a1-8eae-41db-beee-492f7c721ae1?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/4559)
Author
Owner

@iamsg97 commented on GitHub (Sep 17, 2025):

Can you try it locally from the canary branch? Look at the commit done by @frectonz
I think this should resolve the issue! But let me know if not, I am still trying to check in local, and if not will update here with a fix!

@iamsg97 commented on GitHub (Sep 17, 2025): Can you try it locally from the canary branch? Look at the [commit](https://github.com/better-auth/better-auth/commit/ea89d22ee2932c175c14fdc4d9340eed67a278e8#diff-3554663291040ac1167722a655653d83f5529b83bbb69eadb42b56dbc3ae7f93) done by @frectonz I think this should resolve the issue! But let me know if not, I am still trying to check in local, and if not will update here with a fix!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1896