mongodb: Incorrect type for activeOrganizationId field of session when using organization plugin #1190

Closed
opened 2026-03-13 08:27:13 -05:00 by GiteaMirror · 6 comments
Owner

Originally created by @adarshmadrecha on GitHub (May 10, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Use mongodb database and enable organization plugin.

Current vs. Expected behavior

Organization ID is Object ID

Image

However, the activeOrganizationId in the session collection is string. It should be ObjectId

Image

What version of Better Auth are you using?

1.2.7

Provide environment information

- Database: MongoDB

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

Backend

Auth config (if applicable)

import { MongoClient } from 'mongodb';

export const auth = betterAuth({
  database: mongodbAdapter(db),
  emailAndPassword: {
    enabled: true,
  },
  plugins: [
    organization(),
  ],
});

Additional context

No response

Originally created by @adarshmadrecha on GitHub (May 10, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce Use mongodb database and enable organization plugin. ### Current vs. Expected behavior Organization ID is Object ID <img width="421" alt="Image" src="https://github.com/user-attachments/assets/e16450df-96d9-4f47-9007-274d912303de" /> However, the `activeOrganizationId` in the `session` collection is string. It should be `ObjectId` <img width="479" alt="Image" src="https://github.com/user-attachments/assets/26ab8553-6219-4b78-97cd-64868c37edbc" /> ### What version of Better Auth are you using? 1.2.7 ### Provide environment information ```bash - Database: MongoDB ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { MongoClient } from 'mongodb'; export const auth = betterAuth({ database: mongodbAdapter(db), emailAndPassword: { enabled: true, }, plugins: [ organization(), ], }); ``` ### Additional context _No response_
Author
Owner

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

Hi, @adarshmadrecha. 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 in Better Auth v1.2.7, the activeOrganizationId field in the session collection is stored as a string instead of an ObjectId when using the organization plugin with MongoDB.
  • This causes a type inconsistency since the organization ID itself is an ObjectId.
  • There have been no comments or updates on this issue so far.

Next Steps:

  • Please let me know if this issue is still relevant with the latest version of better-auth by commenting here.
  • If I don’t hear back within 7 days, this issue will be automatically closed.

Thank you for your understanding and contribution!

@dosubot[bot] commented on GitHub (Aug 9, 2025): Hi, @adarshmadrecha. 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 in Better Auth v1.2.7, the activeOrganizationId field in the session collection is stored as a string instead of an ObjectId when using the organization plugin with MongoDB. - This causes a type inconsistency since the organization ID itself is an ObjectId. - There have been no comments or updates on this issue so far. Next Steps: - Please let me know if this issue is still relevant with the latest version of better-auth by commenting here. - If I don’t hear back within 7 days, this issue will be automatically closed. Thank you for your understanding and contribution!
Author
Owner

@adarshmadrecha commented on GitHub (Aug 9, 2025):

Yes this issue is still present and I believe should be fixed

@adarshmadrecha commented on GitHub (Aug 9, 2025): Yes this issue is still present and I believe should be fixed
Author
Owner

@ping-maxwell commented on GitHub (Oct 1, 2025):

Looking deeper, this isn't just a mongo adapter issue. It seems we don't set activeOrganizationId as a reference field.

Image

I'll ask the team why this is, and if I get a response I'll post here.

@ping-maxwell commented on GitHub (Oct 1, 2025): Looking deeper, this isn't just a mongo adapter issue. It seems we don't set `activeOrganizationId` as a reference field. <img width="817" height="146" alt="Image" src="https://github.com/user-attachments/assets/17e540ed-36a4-42a9-b88a-436e8343b883" /> I'll ask the team why this is, and if I get a response I'll post here.
Author
Owner

@ping-maxwell commented on GitHub (Oct 1, 2025):

In the mean time I've opened a PR here:
https://github.com/better-auth/better-auth/pull/5011

@ping-maxwell commented on GitHub (Oct 1, 2025): In the mean time I've opened a PR here: https://github.com/better-auth/better-auth/pull/5011
Author
Owner

@ping-maxwell commented on GitHub (Oct 1, 2025):

I'll ask the team why this is, and if I get a response I'll post here.

In general it's better to keep nullable fields to not be a reference

@ping-maxwell commented on GitHub (Oct 1, 2025): > I'll ask the team why this is, and if I get a response I'll post here. In general it's better to keep nullable fields to not be a reference
Author
Owner

@adarshmadrecha commented on GitHub (Dec 15, 2025):

@ping-maxwell It is not about be a reference.

The issue was to store the value as ObjectId rather than string.

@adarshmadrecha commented on GitHub (Dec 15, 2025): @ping-maxwell It is not about `be a reference`. The issue was to store the value as ObjectId rather than string.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1190