[GH-ISSUE #3466] setActiveOrganization inefficient #9613

Closed
opened 2026-04-13 05:10:21 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @wottpal on GitHub (Jul 19, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/3466

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Currently, simply to check whether a member is part of an organization setActiveOrganization internally triggers a findFullOrganization call which unnecessarily queries all mebers of the organization and then tries to find the membership within this error.

93698afd45/packages/better-auth/src/plugins/organization/routes/crud-org.ts (L678-L691)

This is slow in big organizations. Instead, it should simply try to find a member (by user id and org id) in the database. The full organization object is not used in any other way, so this change should be a no brainer.

Current vs. Expected behavior

Current: Inefficiently fetches all members on each set-active call
Expected: Does a single membership verification db query.

What version of Better Auth are you using?

1.2.12

Provide environment information

n/a

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

Client, Backend

Auth config (if applicable)

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

Additional context

No response

Originally created by @wottpal on GitHub (Jul 19, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/3466 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce Currently, simply to check whether a member is part of an organization `setActiveOrganization` internally triggers a `findFullOrganization` call which unnecessarily queries _all_ mebers of the organization and then tries to find the membership within this error. https://github.com/better-auth/better-auth/blob/93698afd456fdb94909999e14fbc7ea446b2be24/packages/better-auth/src/plugins/organization/routes/crud-org.ts#L678-L691 This is slow in big organizations. Instead, it should simply try to find a member (by user id and org id) in the database. The full organization object is not used in any other way, so this change should be a no brainer. ### Current vs. Expected behavior Current: Inefficiently fetches all members on each `set-active` call Expected: Does a single membership verification db query. ### What version of Better Auth are you using? 1.2.12 ### Provide environment information ```bash n/a ``` ### Which area(s) are affected? (Select all that apply) Client, Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, }); ``` ### Additional context _No response_
GiteaMirror added the locked label 2026-04-13 05:10:21 -05:00
Author
Owner

@wottpal commented on GitHub (Jul 19, 2025):

Btw happy to PR this if desired

<!-- gh-comment-id:3092304921 --> @wottpal commented on GitHub (Jul 19, 2025): Btw happy to PR this if desired
Author
Owner

@Kinfe123 commented on GitHub (Jul 21, 2025):

Already fixed via #3497

<!-- gh-comment-id:3095512164 --> @Kinfe123 commented on GitHub (Jul 21, 2025): Already fixed via #3497
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#9613