[GH-ISSUE #3837] setActiveOrganization does not cause update in other react hooks #27061

Closed
opened 2026-04-17 17:51:40 -05:00 by GiteaMirror · 6 comments
Owner

Originally created by @dnil-io on GitHub (Aug 6, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/3837

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

If I use the following example in react to determine the selected organisation:

const { data } = authClient.useSession();
console.log(data?.session.activeOrganizationId);

and do

await authClient.organization.setActive({ organizationId: orgId });

I would expect the useSession() hook to cause an update. But this does not happen :/ I know I can use useActiveOrganization which causes an update but this is one networkrequest more which I would prefer not having to do.

Current vs. Expected behavior

Current:
const { data } = authClient.useSession();
No update on setActive(...)

Expected:
const { data } = authClient.useSession();
Update on setActive(...)

What version of Better Auth are you using?

1.3.4

Provide environment information

Not relevant

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

Client

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
  //org plugin enabled
});

Additional context

No response

Originally created by @dnil-io on GitHub (Aug 6, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/3837 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce If I use the following example in react to determine the selected organisation: ``` const { data } = authClient.useSession(); console.log(data?.session.activeOrganizationId); ``` and do ` await authClient.organization.setActive({ organizationId: orgId }); ` I would expect the useSession() hook to cause an update. But this does not happen :/ I know I can use useActiveOrganization which causes an update but this is one networkrequest more which I would prefer not having to do. ### Current vs. Expected behavior Current: const { data } = authClient.useSession(); No update on setActive(...) Expected: const { data } = authClient.useSession(); Update on setActive(...) ### What version of Better Auth are you using? 1.3.4 ### Provide environment information ```bash Not relevant ``` ### Which area(s) are affected? (Select all that apply) Client ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, //org plugin enabled }); ``` ### Additional context _No response_
GiteaMirror added the locked label 2026-04-17 17:51:40 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Aug 6, 2025):

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

This is a known architectural limitation in Better Auth: calling setActiveOrganization does not automatically update the session object returned by useSession(). The session context is determined by the session or cookies, and useSession() only updates when the session itself is refreshed or refetched—not when the active organization changes via setActiveOrganization. This is why useActiveOrganization updates reactively (with an extra network request), but useSession() does not (sourcesourcesource).

Workarounds include manually refetching the session after calling setActiveOrganization, forcing a component re-render, or using useActiveOrganization despite the extra request. Also, avoid mixing destructured and direct object methods from the auth client, as this can break reactivity in useSession() (source).

There’s no recent change in v1.3.4 that addresses this—it's not considered a bug, just a limitation of the current design. If you need code samples or more technical details, let me know!

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:3161375831 --> @dosubot[bot] commented on GitHub (Aug 6, 2025): <!-- Greeting --> Hi @dnil-io! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> This is a known architectural limitation in Better Auth: calling setActiveOrganization does not automatically update the session object returned by useSession(). The session context is determined by the session or cookies, and useSession() only updates when the session itself is refreshed or refetched—not when the active organization changes via setActiveOrganization. This is why useActiveOrganization updates reactively (with an extra network request), but useSession() does not ([source](https://github.com/better-auth/better-auth/issues/2309), [source](https://github.com/better-auth/better-auth/issues/769), [source](https://github.com/better-auth/better-auth/issues/3452)). Workarounds include manually refetching the session after calling setActiveOrganization, forcing a component re-render, or using useActiveOrganization despite the extra request. Also, avoid mixing destructured and direct object methods from the auth client, as this can break reactivity in useSession() ([source](https://github.com/better-auth/better-auth/issues/1006)). There’s no recent change in v1.3.4 that addresses this—it's not considered a bug, just a limitation of the current design. If you need code samples or more technical details, let me know! <!-- 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/cba9cef4-7b3e-42b0-9b36-66dde6530a97?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/cba9cef4-7b3e-42b0-9b36-66dde6530a97?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/cba9cef4-7b3e-42b0-9b36-66dde6530a97?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/cba9cef4-7b3e-42b0-9b36-66dde6530a97?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/cba9cef4-7b3e-42b0-9b36-66dde6530a97?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/cba9cef4-7b3e-42b0-9b36-66dde6530a97?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/cba9cef4-7b3e-42b0-9b36-66dde6530a97?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/3837)
Author
Owner

@dnil-io commented on GitHub (Aug 6, 2025):

I do not agree completely to be honest @Dosu. I am hoping a human will look into this as well. I think it could be an easy fix, especially since unlike Dosu claims, the activeSession is not stored inside of the cookies. The initial state is set by the get-session request, that is correct. But I do not think that optimistically updating the session if the server returns 200 on the setActiveOrganisation is the worst thing to do.. Especially since this is a hook and not the getSession()-method. If this is really not possible, at least a warning in the documentation would be nice that the hook is only mildly reactive and a fancy version of the getSession as a query. Also I was not successful finding how to trigger refetching of the session behind useSession. Maybe you could actually be helpful here @Dosu and help me out ;)

But it is true that it is probably also affecting other parts of the session object probably. I do not know if they get

Also, avoid mixing destructured and direct object methods from the auth client, as this can break reactivity in useSession() (https://github.com/better-auth/better-auth/issues/1006).

This part is completely halucinated though I believe.

<!-- gh-comment-id:3161443484 --> @dnil-io commented on GitHub (Aug 6, 2025): I do not agree completely to be honest @Dosu. I am hoping a human will look into this as well. I think it could be an easy fix, especially since unlike Dosu claims, the activeSession is not stored inside of the cookies. The initial state is set by the get-session request, that is correct. But I do not think that optimistically updating the session if the server returns 200 on the setActiveOrganisation is the worst thing to do.. Especially since this is a hook and not the getSession()-method. If this is really not possible, at least a warning in the documentation would be nice that the hook is only mildly reactive and a fancy version of the getSession as a query. Also I was not successful finding how to trigger refetching of the session behind useSession. Maybe you could actually be helpful here @Dosu and help me out ;) But it is true that it is probably also affecting other parts of the session object probably. I do not know if they get > Also, avoid mixing destructured and direct object methods from the auth client, as this can break reactivity in useSession() (https://github.com/better-auth/better-auth/issues/1006). This part is completely halucinated though I believe.
Author
Owner

@dosubot[bot] commented on GitHub (Aug 6, 2025):

You can manually trigger a session update by calling the refetch method returned from useSession(). For example:

const { data, refetch } = authClient.useSession();
// After setActiveOrganization:
await authClient.organization.setActive({ organizationId: orgId });
refetch();

This is the officially supported way to refresh the session after actions like setActiveOrganization—there’s no built-in optimistic update, so a refetch is needed to sync the session state with the server response. The session is managed in-memory and updated via the get-session endpoint, not cookies, so your understanding is correct on that point. Optimistic updates aren’t currently supported in the hook itself ref.

On the destructuring/direct method point: it’s not a hallucination—mixing destructured and direct object methods from the auth client can break reactivity in useSession() due to how object references are tracked internally. Stick to either destructured or direct usage consistently to avoid subtle bugs ref.

There’s no explicit documentation warning about this limitation yet, but maintainers have acknowledged the need for better docs. Other session fields are subject to the same reactivity caveats—use refetch as a general workaround when you need up-to-date session data.

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:3161455738 --> @dosubot[bot] commented on GitHub (Aug 6, 2025): <!-- Answer --> You can manually trigger a session update by calling the refetch method returned from useSession(). For example: ```js const { data, refetch } = authClient.useSession(); // After setActiveOrganization: await authClient.organization.setActive({ organizationId: orgId }); refetch(); ``` This is the officially supported way to refresh the session after actions like setActiveOrganization—there’s no built-in optimistic update, so a refetch is needed to sync the session state with the server response. The session is managed in-memory and updated via the get-session endpoint, not cookies, so your understanding is correct on that point. Optimistic updates aren’t currently supported in the hook itself [ref](https://github.com/better-auth/better-auth/issues/3608). On the destructuring/direct method point: it’s not a hallucination—mixing destructured and direct object methods from the auth client can break reactivity in useSession() due to how object references are tracked internally. Stick to either destructured or direct usage consistently to avoid subtle bugs [ref](https://github.com/better-auth/better-auth/issues/1006). There’s no explicit documentation warning about this limitation yet, but maintainers have acknowledged the need for better docs. Other session fields are subject to the same reactivity caveats—use refetch as a general workaround when you need up-to-date session data. <!-- 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/33e76c8a-98d6-45a4-bbac-5819e07a4ca3?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/33e76c8a-98d6-45a4-bbac-5819e07a4ca3?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/33e76c8a-98d6-45a4-bbac-5819e07a4ca3?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/33e76c8a-98d6-45a4-bbac-5819e07a4ca3?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/33e76c8a-98d6-45a4-bbac-5819e07a4ca3?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/33e76c8a-98d6-45a4-bbac-5819e07a4ca3?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/33e76c8a-98d6-45a4-bbac-5819e07a4ca3?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/3837)
Author
Owner

@himself65 commented on GitHub (Aug 7, 2025):

maybe related: https://github.com/better-auth/better-auth/issues/1006

<!-- gh-comment-id:3162661607 --> @himself65 commented on GitHub (Aug 7, 2025): maybe related: https://github.com/better-auth/better-auth/issues/1006
Author
Owner

@issam-seghir commented on GitHub (Aug 7, 2025):

discussion #3860

<!-- gh-comment-id:3165067216 --> @issam-seghir commented on GitHub (Aug 7, 2025): [discussion #3860](https://github.com/better-auth/better-auth/discussions/3860)
Author
Owner

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

Hi, @dnil-io. I'm Dosu, and I'm helping the better-auth team manage their backlog and am marking this issue as stale.

Issue Summary:

  • You reported that calling setActiveOrganization via authClient.organization.setActive does not update the useSession() React hook automatically.
  • useActiveOrganization updates correctly but triggers an extra network request.
  • The core limitation is that useSession() only updates on session refetch, not automatically after setActiveOrganization.
  • The recommended workaround is to manually call refetch() from useSession() after setActiveOrganization to keep state in sync.
  • You requested clearer documentation on this behavior, and the discussion also touched on potential reactivity issues when mixing destructured and direct auth client methods (#1006).

Next Steps:

  • Please confirm if this issue is still relevant with the latest version of better-auth; if so, feel free to keep the discussion open by commenting here.
  • Otherwise, I will automatically close this issue in 7 days.

Thanks for your understanding and contribution!

<!-- gh-comment-id:3498171092 --> @dosubot[bot] commented on GitHub (Nov 6, 2025): Hi, @dnil-io. I'm [Dosu](https://dosu.dev), and I'm helping the better-auth team manage their backlog and am marking this issue as stale. **Issue Summary:** - You reported that calling `setActiveOrganization` via `authClient.organization.setActive` does not update the `useSession()` React hook automatically. - `useActiveOrganization` updates correctly but triggers an extra network request. - The core limitation is that `useSession()` only updates on session refetch, not automatically after `setActiveOrganization`. - The recommended workaround is to manually call `refetch()` from `useSession()` after `setActiveOrganization` to keep state in sync. - You requested clearer documentation on this behavior, and the discussion also touched on potential reactivity issues when mixing destructured and direct auth client methods (#1006). **Next Steps:** - Please confirm if this issue is still relevant with the latest version of better-auth; if so, feel free to keep the discussion open by commenting here. - Otherwise, I will automatically close this issue in 7 days. Thanks for your understanding and contribution!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#27061