[GH-ISSUE #3490] v1.3.0 activeOrganizationId not present in session inference #18252

Closed
opened 2026-04-15 16:39:59 -05:00 by GiteaMirror · 5 comments
Owner

Originally created by @ubinatus on GitHub (Jul 20, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/3490

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Ex:

  1. Create a basic auth config with organization plugin
  2. Infer the Session type using typeof auth.$Infer.Session.session
  3. Check that activeOrganizationId is not appearing.

Current vs. Expected behavior

Current:

type Session = typeof auth.$Infer.Session.session
// Session["activeOrganizationId"] <-- Not valid

Expected:

type Session = typeof auth.$Infer.Session.session
// Session["activeOrganizationId"] <-- string | null

What version of Better Auth are you using?

1.3.0

Provide environment information

-

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

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth"
import { organization } from "better-auth/plugins"
export const auth = betterAuth({
  plugins: [organization()]
});

Additional context

No response

Originally created by @ubinatus on GitHub (Jul 20, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/3490 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce Ex: 1. Create a basic auth config with organization plugin 2. Infer the Session type using `typeof auth.$Infer.Session.session` 3. Check that `activeOrganizationId` is not appearing. ### Current vs. Expected behavior Current: ```typescript type Session = typeof auth.$Infer.Session.session // Session["activeOrganizationId"] <-- Not valid ``` Expected: ```typescript type Session = typeof auth.$Infer.Session.session // Session["activeOrganizationId"] <-- string | null ``` ### What version of Better Auth are you using? 1.3.0 ### Provide environment information ```bash - ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" import { organization } from "better-auth/plugins" export const auth = betterAuth({ plugins: [organization()] }); ``` ### Additional context _No response_
GiteaMirror added the locked label 2026-04-15 16:39:59 -05:00
Author
Owner

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

This is a known TypeScript type inference limitation in Better Auth v1.3.0. The organization plugin does add an activeOrganizationId field to the session schema, but when plugins are passed as a generic array, TypeScript loses the specific type information, so activeOrganizationId doesn't appear in the inferred session type. This isn't a runtime bug—it's a structural limitation of the current plugin/type system [source].

To work around this, you can explicitly set activeOrganizationId in your session using a databaseHooks.session.create.before hook:

databaseHooks: {
  session: {
    create: {
      before: async (session, ctx) => {
        // fetch the user's organization here
        return {
          data: {
            ...session,
            activeOrganizationId: /* your org id logic */,
          },
        };
      }
    }
  }
}

[docs example]

Alternatively, you can manually cast the session type or use TypeScript assertions where needed if type inference fails. For more robust type inference, define your Better Auth options separately (using satisfies BetterAuthOptions) and pass those options to your plugins—this can help TypeScript preserve plugin-added fields [source].

Let me know if you want a concrete example for your setup!

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Join Discord Share on X

<!-- gh-comment-id:3092665228 --> @dosubot[bot] commented on GitHub (Jul 20, 2025): <!-- Answer --> This is a known TypeScript type inference limitation in Better Auth v1.3.0. The organization plugin does add an `activeOrganizationId` field to the session schema, but when plugins are passed as a generic array, TypeScript loses the specific type information, so `activeOrganizationId` doesn't appear in the inferred session type. This isn't a runtime bug—it's a structural limitation of the current plugin/type system [[source](https://github.com/better-auth/better-auth/issues/3233)]. To work around this, you can explicitly set `activeOrganizationId` in your session using a `databaseHooks.session.create.before` hook: ```ts databaseHooks: { session: { create: { before: async (session, ctx) => { // fetch the user's organization here return { data: { ...session, activeOrganizationId: /* your org id logic */, }, }; } } } } ``` [[docs example](https://github.com/better-auth/better-auth/blob/adee10d5229c6cb156095de416820aa5082ae4aa/docs/content/docs/plugins/organization.mdx)] Alternatively, you can manually cast the session type or use TypeScript assertions where needed if type inference fails. For more robust type inference, define your Better Auth options separately (using `satisfies BetterAuthOptions`) and pass those options to your plugins—this can help TypeScript preserve plugin-added fields [[source](https://github.com/better-auth/better-auth/issues/3233)]. Let me know if you want a concrete example for your setup! <!-- 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/5bda9184-0814-4b7f-b9c7-a4b97a5bea94?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/5bda9184-0814-4b7f-b9c7-a4b97a5bea94?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/5bda9184-0814-4b7f-b9c7-a4b97a5bea94?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/5bda9184-0814-4b7f-b9c7-a4b97a5bea94?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/5bda9184-0814-4b7f-b9c7-a4b97a5bea94?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/5bda9184-0814-4b7f-b9c7-a4b97a5bea94?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/5bda9184-0814-4b7f-b9c7-a4b97a5bea94?feedback_type=other)</sup>&nbsp;&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/3490)
Author
Owner

@sebboer commented on GitHub (Jul 29, 2025):

This is problem is still present in 1.3.4. @ubinatus is this fixed for you?

<!-- gh-comment-id:3134096682 --> @sebboer commented on GitHub (Jul 29, 2025): This is problem is still present in `1.3.4`. @ubinatus is this fixed for you?
Author
Owner

@ubinatus commented on GitHub (Jul 29, 2025):

This is problem is still present in 1.3.4. @ubinatus is this fixed for you?

It got solved, yeah!

<!-- gh-comment-id:3134256069 --> @ubinatus commented on GitHub (Jul 29, 2025): > This is problem is still present in `1.3.4`. @ubinatus is this fixed for you? It got solved, yeah!
Author
Owner

@cpolanish commented on GitHub (Aug 22, 2025):

I'm still seeing this issue in 1.3.7

<!-- gh-comment-id:3215419785 --> @cpolanish commented on GitHub (Aug 22, 2025): I'm still seeing this issue in 1.3.7
Author
Owner

@corydeppen commented on GitHub (Mar 12, 2026):

Looks like this issue is still present using 1.5.5. Can anyone else confirm and/or mention what's resolved the type issue? I don't even see the activeOrganizationId field returned in the session data despite it being included in the the schema and db, so it's a bit confusing at the moment.

<!-- gh-comment-id:4047379503 --> @corydeppen commented on GitHub (Mar 12, 2026): Looks like this issue is still present using 1.5.5. Can anyone else confirm and/or mention what's resolved the type issue? I don't even see the `activeOrganizationId` field returned in the session data despite it being included in the the schema and db, so it's a bit confusing at the moment.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#18252