[GH-ISSUE #3695] Setting Active Organization not working with MongoDB #9691

Closed
opened 2026-04-13 05:19:47 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @TheUntraceable on GitHub (Jul 29, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/3695

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Setup better auth with mongodb adapter and organization plugin
  2. Create a new org
  3. Try to set that org as the active one
    https://codesandbox.io/p/devbox/l6c97p <- Fully setup, try the preview in a new tab (the MongoDB url setup there is newly made)

Current vs. Expected behavior

Current - It says that member is not part of the organization.
Expected - Sets the active organization just fine.

What version of Better Auth are you using?

1.3.4

Provide environment information

- OS: Windows 11
- Browser: Brave

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

Backend

Auth config (if applicable)

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

https://codesandbox.io/p/devbox/l6c97p

Additional context

I made this codesandbox for testing: https://codesandbox.io/p/devbox/l6c97p

Originally created by @TheUntraceable on GitHub (Jul 29, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/3695 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. Setup better auth with mongodb adapter and organization plugin 2. Create a new org 3. Try to set that org as the active one https://codesandbox.io/p/devbox/l6c97p <- Fully setup, try the preview in a new tab (the MongoDB url setup there is newly made) ### Current vs. Expected behavior Current - It says that member is not part of the organization. Expected - Sets the active organization just fine. ### What version of Better Auth are you using? 1.3.4 ### Provide environment information ```bash - OS: Windows 11 - Browser: Brave ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, }); https://codesandbox.io/p/devbox/l6c97p ``` ### Additional context I made this codesandbox for testing: https://codesandbox.io/p/devbox/l6c97p
GiteaMirror added the locked label 2026-04-13 05:19:47 -05:00
Author
Owner

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

This is a known issue with Better Auth v1.3.4 when using the MongoDB adapter: the membership check for setting the active organization can fail due to a type mismatch between string IDs and MongoDB ObjectIds, causing the "member is not part of the organization" error. This is a common pitfall in MongoDB adapter implementations, as IDs may be stored as ObjectIds but compared as strings in queries, or vice versa, leading to failed lookups details.

Recent versions of Better Auth (after v1.3.4) include important fixes for this, such as improved membership check logic and ID handling (PR #3497, PR #3509, PR #3593). Upgrading to the latest version is the most reliable way to resolve this.

If you need a workaround in v1.3.4, check that your MongoDB collections store and query userId and organizationId consistently (all as strings or all as ObjectIds). Also, make sure the organization plugin is included in your server-side auth config and that teams are enabled in your client config example.

If the issue is session-related, you can use a session.create.before hook in your databaseHooks config to explicitly set activeOrganizationId during session creation by querying the member table for the user's organization membership example code:

databaseHooks: {
  session: {
    create: {
      before: async (session, ctx) => {
        const membership = await db.member.findFirst({
          where: { userId: session.userId },
          include: { organization: true },
          orderBy: { createdAt: 'asc' }
        });

        if (!membership) {
          return { data: session };
        }

        return {
          data: {
            ...session,
            activeOrganizationId: membership.organization.id,
          },
        };
      }
    }
  }
}

If these steps don't resolve it, upgrading to the latest Better Auth version is strongly recommended, as the recent fixes directly address this problem.

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:3134435435 --> @dosubot[bot] commented on GitHub (Jul 30, 2025): <!-- Answer --> This is a known issue with Better Auth v1.3.4 when using the MongoDB adapter: the membership check for setting the active organization can fail due to a type mismatch between string IDs and MongoDB ObjectIds, causing the "member is not part of the organization" error. This is a common pitfall in MongoDB adapter implementations, as IDs may be stored as ObjectIds but compared as strings in queries, or vice versa, leading to failed lookups [details](https://github.com/better-auth/better-auth/issues/3233). Recent versions of Better Auth (after v1.3.4) include important fixes for this, such as improved membership check logic and ID handling ([PR #3497](https://github.com/better-auth/better-auth/pull/3497), [PR #3509](https://github.com/better-auth/better-auth/pull/3509), [PR #3593](https://github.com/better-auth/better-auth/pull/3593)). Upgrading to the latest version is the most reliable way to resolve this. If you need a workaround in v1.3.4, check that your MongoDB collections store and query userId and organizationId consistently (all as strings or all as ObjectIds). Also, make sure the organization plugin is included in your server-side auth config and that teams are enabled in your client config [example](https://github.com/better-auth/better-auth/issues/3079). If the issue is session-related, you can use a `session.create.before` hook in your `databaseHooks` config to explicitly set `activeOrganizationId` during session creation by querying the member table for the user's organization membership [example code](https://github.com/better-auth/better-auth/issues/3233): ```typescript databaseHooks: { session: { create: { before: async (session, ctx) => { const membership = await db.member.findFirst({ where: { userId: session.userId }, include: { organization: true }, orderBy: { createdAt: 'asc' } }); if (!membership) { return { data: session }; } return { data: { ...session, activeOrganizationId: membership.organization.id, }, }; } } } } ``` If these steps don't resolve it, upgrading to the latest Better Auth version is strongly recommended, as the recent fixes directly address this problem. <!-- 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/584fe987-3700-457f-afdb-f287da00b7ee?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/584fe987-3700-457f-afdb-f287da00b7ee?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/584fe987-3700-457f-afdb-f287da00b7ee?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/584fe987-3700-457f-afdb-f287da00b7ee?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/584fe987-3700-457f-afdb-f287da00b7ee?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/584fe987-3700-457f-afdb-f287da00b7ee?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/584fe987-3700-457f-afdb-f287da00b7ee?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/3695)
Author
Owner

@Kinfe123 commented on GitHub (Aug 3, 2025):

should be fixed by now via #3757. if not let me now i will reopen this.

<!-- gh-comment-id:3148500259 --> @Kinfe123 commented on GitHub (Aug 3, 2025): should be fixed by now via #3757. if not let me now i will reopen this.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#9691