customSession plugin does not infer additionalFields in session typing #1180

Closed
opened 2026-03-13 08:26:47 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @snelusha on GitHub (May 8, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

import { betterAuth } from "better-auth";

export const auth = betterAuth({
  session: {
    additionalFields: {
      activeAccountId: {
        type: "string",
        defaultValue: null,
        returned: true,
      },
    },
  },
  plugins: [
    customSession(async ({ session }) => {
      const activeAccountId = session.activeAccountId;
      // TS Error: Property 'activeAccountId' does not exist on type 'Session'
      return { session };
    }),
  ],
});
  1. Define an additionalField in session.additionalFields.
  2. Use the customSession plugin to access the custom field.

Current vs. Expected behavior

Current:
TypeScript does not recognize the activeAccountId field on the session object inside customSession, causing a type error.

Expected:
Since activeAccountId is added via additionalFields and exists at runtime, it should be properly inferred and accessible without type assertion or casting.

What version of Better Auth are you using?

1.2.7

Provide environment information

- TypeScript: 5.8.3

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

Backend, Types

Auth config (if applicable)

import { betterAuth } from "better-auth";

export const auth = betterAuth({
  session: {
    additionalFields: {
      activeAccountId: {
        type: "string",
        defaultValue: null,
        returned: true,
      },
    },
  },
  plugins: [
    customSession(async ({ session }) => {
      const activeAccountId = session.activeAccountId;
      return { session };
    }),
  ],
});

Additional context

I logged the session inside the customSession plugin and confirmed that activeAccountId is present at runtime

Originally created by @snelusha on GitHub (May 8, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce ``` import { betterAuth } from "better-auth"; export const auth = betterAuth({ session: { additionalFields: { activeAccountId: { type: "string", defaultValue: null, returned: true, }, }, }, plugins: [ customSession(async ({ session }) => { const activeAccountId = session.activeAccountId; // TS Error: Property 'activeAccountId' does not exist on type 'Session' return { session }; }), ], }); ``` 1. Define an additionalField in `session.additionalFields`. 2. Use the `customSession` plugin to access the custom field. ### Current vs. Expected behavior Current: TypeScript does not recognize the `activeAccountId` field on the session object inside `customSession`, causing a type error. Expected: Since `activeAccountId` is added via `additionalFields` and exists at runtime, it should be properly inferred and accessible without type assertion or casting. ### What version of Better Auth are you using? 1.2.7 ### Provide environment information ```bash - TypeScript: 5.8.3 ``` ### Which area(s) are affected? (Select all that apply) Backend, Types ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth"; export const auth = betterAuth({ session: { additionalFields: { activeAccountId: { type: "string", defaultValue: null, returned: true, }, }, }, plugins: [ customSession(async ({ session }) => { const activeAccountId = session.activeAccountId; return { session }; }), ], }); ``` ### Additional context I logged the session inside the `customSession` plugin and confirmed that `activeAccountId` is present at runtime
Author
Owner

@mfazail commented on GitHub (May 8, 2025):

You can check Some Caveats: section here for customizing session and for infering types of additionalFields.

@mfazail commented on GitHub (May 8, 2025): You can check `Some Caveats:` section [here](https://www.better-auth.com/docs/concepts/session-management#customizing-session-response) for customizing session and for infering types of additionalFields.
Author
Owner

@issam-seghir commented on GitHub (Sep 9, 2025):

I follow the session doc but custom session plugin still not including additional fields

export const authClient = createAuthClient({
    /** The base URL of the server (optional if you're using the same domain) */
    plugins: [
        inferAdditionalFields<typeof auth>(),
        customSessionClient<typeof auth>(),
        magicLinkClient(),
        emailOTPClient(),
        passkeyClient(),
        adminClient(),
    ],
    fetchOptions: {
        onError: async (context) => {
            const { response } = context;
            if (response.status === 429) {
                const retryAfter = response.headers.get("X-Retry-After");
                console.log(`Rate limit exceeded. Retry after ${retryAfter} seconds`);
            }
        },
    },
});

Image
@issam-seghir commented on GitHub (Sep 9, 2025): I follow the session doc but custom session plugin still not including additional fields ```ts export const authClient = createAuthClient({ /** The base URL of the server (optional if you're using the same domain) */ plugins: [ inferAdditionalFields<typeof auth>(), customSessionClient<typeof auth>(), magicLinkClient(), emailOTPClient(), passkeyClient(), adminClient(), ], fetchOptions: { onError: async (context) => { const { response } = context; if (response.status === 429) { const retryAfter = response.headers.get("X-Retry-After"); console.log(`Rate limit exceeded. Retry after ${retryAfter} seconds`); } }, }, }); ``` <img width="693" height="242" alt="Image" src="https://github.com/user-attachments/assets/e375a4e8-1854-42b9-900d-411f05ce9f34" />
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1180