[GH-ISSUE #4222] activeOrganizationId still not present in session inference #18499

Closed
opened 2026-04-15 16:58:47 -05:00 by GiteaMirror · 8 comments
Owner

Originally created by @cpolanish on GitHub (Aug 25, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/4222

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Create an auth config with the organization plugin.
  2. Infer the session type using typeof auth.$infer.Session.session
  3. activeOrganizationId is not present in the resulting type.

Current vs. Expected behavior

activeOrganizationId should be present in the inferred type

What version of Better Auth are you using?

1.3.7

System info

n/a

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

Types

Auth config (if applicable)

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

Additional context

This is related to #3490 which was closed as completed in #3491 however I am still seeing this issue in 1.3.7.

Originally created by @cpolanish on GitHub (Aug 25, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/4222 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1) Create an auth config with the organization plugin. 2) Infer the session type using typeof auth.$infer.Session.session 3) activeOrganizationId is not present in the resulting type. ### Current vs. Expected behavior activeOrganizationId should be present in the inferred type ### What version of Better Auth are you using? 1.3.7 ### System info ```bash n/a ``` ### Which area(s) are affected? (Select all that apply) Types ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" import { organization } from "better-auth/plugins" export const auth = betterAuth({ plugins: [organization()] }); ``` ### Additional context This is related to #3490 which was closed as completed in #3491 however I am still seeing this issue in 1.3.7.
GiteaMirror added the locked label 2026-04-15 16:58:47 -05:00
Author
Owner

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

This is a known TypeScript inference limitation in Better Auth, even after the fix in #3491. When using plugins like organization, TypeScript can lose track of fields like activeOrganizationId in the inferred session type, especially if plugins are passed as a generic array or if you use customSession in a certain order. This isn't a runtime bug, but a structural limitation of the current plugin/type system [source].

Workarounds:

  • You can explicitly set activeOrganizationId at runtime using a databaseHooks.session.create.before hook, which ensures the field is present in the session object even if TypeScript doesn't infer it [source]:

    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,
              },
            };
          }
        }
      }
    }
    
  • For better 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 like activeOrganizationId [source].

  • If you need to, you can manually cast the session type or use TypeScript assertions where inference fails.

Plugin order matters: if you use customSession, make sure it's after organization in your plugins array, or follow the pattern above to preserve type information.

If you share your full plugin config, I can help check for any other inference pitfalls.

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:3220845543 --> @dosubot[bot] commented on GitHub (Aug 25, 2025): <!-- Answer --> This is a known TypeScript inference limitation in Better Auth, even after the fix in #3491. When using plugins like organization, TypeScript can lose track of fields like `activeOrganizationId` in the inferred session type, especially if plugins are passed as a generic array or if you use customSession in a certain order. This isn't a runtime bug, but a structural limitation of the current plugin/type system [[source](https://github.com/better-auth/better-auth/issues/3490#issuecomment-3092665228)]. Workarounds: - You can explicitly set `activeOrganizationId` at runtime using a `databaseHooks.session.create.before` hook, which ensures the field is present in the session object even if TypeScript doesn't infer it [[source](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, }, }; } } } } ``` - For better 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 like `activeOrganizationId` [[source](https://github.com/better-auth/better-auth/issues/3233#issuecomment-3021659951)]. - If you need to, you can manually cast the session type or use TypeScript assertions where inference fails. Plugin order matters: if you use `customSession`, make sure it's after `organization` in your plugins array, or follow the pattern above to preserve type information. If you share your full plugin config, I can help check for any other inference pitfalls. <!-- 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/dece5267-3c3f-40ac-9d29-fbd0b5fb48f0?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/dece5267-3c3f-40ac-9d29-fbd0b5fb48f0?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/dece5267-3c3f-40ac-9d29-fbd0b5fb48f0?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/dece5267-3c3f-40ac-9d29-fbd0b5fb48f0?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/dece5267-3c3f-40ac-9d29-fbd0b5fb48f0?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/dece5267-3c3f-40ac-9d29-fbd0b5fb48f0?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/dece5267-3c3f-40ac-9d29-fbd0b5fb48f0?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/4222)
Author
Owner

@Kinfe123 commented on GitHub (Sep 2, 2025):

does this mean the inference is not working for you on the previous version at all ?

<!-- gh-comment-id:3245887832 --> @Kinfe123 commented on GitHub (Sep 2, 2025): does this mean the inference is not working for you on the previous version at all ?
Author
Owner

@cpolanish commented on GitHub (Sep 2, 2025):

Yes, that's correct

<!-- gh-comment-id:3245980762 --> @cpolanish commented on GitHub (Sep 2, 2025): Yes, that's correct
Author
Owner

@Kinfe123 commented on GitHub (Sep 2, 2025):

can you please do a repro for this ?

<!-- gh-comment-id:3246010672 --> @Kinfe123 commented on GitHub (Sep 2, 2025): can you please do a repro for this ?
Author
Owner

@Kinfe123 commented on GitHub (Sep 9, 2025):

are you doing a force assertion on return type of auth export ?

<!-- gh-comment-id:3271429281 --> @Kinfe123 commented on GitHub (Sep 9, 2025): are you doing a force assertion on return type of auth export ?
Author
Owner

@ElasticBottle commented on GitHub (Sep 11, 2025):

@Kinfe123 I believe type assertion is the issue. For e.g., I've seen the following in codebases I've worked with that encounters this issue

const init= () : ReturnType<typeof betterAuth<BetterAuthOptions>> => { // issue is the typing of the return type
    return betterAuth(config);
}
const authAPI = init()
const session = await authAPI.api.getSession({ headers: new Headers() });
session // no activeOrgaization here ever if plugin is configured

is there anyway around this?

<!-- gh-comment-id:3277085108 --> @ElasticBottle commented on GitHub (Sep 11, 2025): @Kinfe123 I believe type assertion is the issue. For e.g., I've seen the following in codebases I've worked with that encounters this issue ``` const init= () : ReturnType<typeof betterAuth<BetterAuthOptions>> => { // issue is the typing of the return type return betterAuth(config); } const authAPI = init() const session = await authAPI.api.getSession({ headers: new Headers() }); session // no activeOrgaization here ever if plugin is configured ``` is there anyway around this?
Author
Owner

@cpolanish commented on GitHub (Sep 11, 2025):

Sorry for the delayed response, yes, I believe I was trying to do something similar to this to try to work around different type issue (I believe it was related to #3067). I've stepped away from this for a bit and now haven't been able to recreate what I was doing unfortunately.

<!-- gh-comment-id:3280798941 --> @cpolanish commented on GitHub (Sep 11, 2025): Sorry for the delayed response, yes, I believe I was trying to do something similar to this to try to work around _different_ type issue (I believe it was related to #3067). I've stepped away from this for a bit and now haven't been able to recreate what I was doing unfortunately.
Author
Owner

@dosubot[bot] commented on GitHub (Dec 11, 2025):

Hi, @cpolanish. 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.3.7, the inferred session type was missing the activeOrganizationId property despite using the organization plugin.
  • This issue persisted even after fix #3491.
  • It was identified as a known TypeScript inference limitation related to plugin usage.
  • Workarounds suggested included explicitly setting activeOrganizationId via database hooks or carefully typing plugin options.
  • You confirmed trying similar workarounds and have not been able to reproduce the issue recently.

Next Steps:

  • Please let me know if this issue is still relevant with the latest version of better-auth by commenting here to keep the discussion open.
  • Otherwise, this issue will be automatically closed in 7 days.

Thanks for your understanding and contribution!

<!-- gh-comment-id:3642667818 --> @dosubot[bot] commented on GitHub (Dec 11, 2025): Hi, @cpolanish. 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.3.7, the inferred session type was missing the `activeOrganizationId` property despite using the organization plugin. - This issue persisted even after fix #3491. - It was identified as a known TypeScript inference limitation related to plugin usage. - Workarounds suggested included explicitly setting `activeOrganizationId` via database hooks or carefully typing plugin options. - You confirmed trying similar workarounds and have not been able to reproduce the issue recently. **Next Steps:** - Please let me know if this issue is still relevant with the latest version of better-auth by commenting here to keep the discussion open. - Otherwise, this issue will be automatically closed 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#18499