useSession doesn't infer types for session data when using admin plugin #1186

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

Originally created by @IamAyaanSk on GitHub (May 9, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Create a next js application with better auth.
  2. Add the admin plugin in both better auth server and client instances.
  3. Use the useSession hook in any client component.
  4. Try accessing the data.user.role.

Current vs. Expected behavior

When accessing the role property in the user object, I got a typescript error stating that role doesn't exist.
Expected behavior:
The admin plugin should automatically extent the session data type when obtaining session data using the useSession hook.

What version of Better Auth are you using?

1.2.7

Provide environment information

- OS: MacOS 15
- Browser: Chrome

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

Client

Auth config (if applicable)

import { betterAuth } from 'better-auth'
import { nextCookies } from 'better-auth/next-js'
import { admin } from 'better-auth/plugins'

export const auth = betterAuth({
  database: .....,
  plugins: [admin(), nextCookies()],
  emailAndPassword: {
    .....
  },
  emailVerification: {
    .....
  }
})


// Client

import { createAuthClient } from 'better-auth/react'
import { adminClient } from 'better-auth/client/plugins'

export const authClient = createAuthClient({
  plugins: [adminClient()]
})

Additional context

No response

Originally created by @IamAyaanSk on GitHub (May 9, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. Create a next js application with better auth. 2. Add the admin plugin in both better auth server and client instances. 3. Use the useSession hook in any client component. 4. Try accessing the data.user.role. ### Current vs. Expected behavior When accessing the role property in the user object, I got a typescript error stating that role doesn't exist. Expected behavior: The admin plugin should automatically extent the session data type when obtaining session data using the useSession hook. ### What version of Better Auth are you using? 1.2.7 ### Provide environment information ```bash - OS: MacOS 15 - Browser: Chrome ``` ### Which area(s) are affected? (Select all that apply) Client ### Auth config (if applicable) ```typescript import { betterAuth } from 'better-auth' import { nextCookies } from 'better-auth/next-js' import { admin } from 'better-auth/plugins' export const auth = betterAuth({ database: ....., plugins: [admin(), nextCookies()], emailAndPassword: { ..... }, emailVerification: { ..... } }) // Client import { createAuthClient } from 'better-auth/react' import { adminClient } from 'better-auth/client/plugins' export const authClient = createAuthClient({ plugins: [adminClient()] }) ``` ### Additional context _No response_
Author
Owner

@depsimon commented on GitHub (May 9, 2025):

I just noticed the same thing, here's a bit more context: the session.user has this type:

(property) user: {
    id: string;
    name: string;
    email: string;
    emailVerified: boolean;
    createdAt: Date;
    updatedAt: Date;
    image?: string | null | undefined | undefined;
    banned: boolean | null | undefined;
    role?: string | null | undefined;
    banReason?: string | null | undefined;
    banExpires?: Date | null | undefined;
} | {
    id: string;
    name: string;
    email: string;
    emailVerified: boolean;
    createdAt: Date;
    updatedAt: Date;
    image?: string | null | undefined | undefined;
    banned: boolean | null | undefined;
    role?: string | null | undefined;
    banReason?: string | null | undefined;
    banExpires?: Date | null | undefined;
}

which is not compatible with the UserWithRole type because role should either be string or undefined.

@depsimon commented on GitHub (May 9, 2025): I just noticed the same thing, here's a bit more context: the `session.user` has this type: ```ts (property) user: { id: string; name: string; email: string; emailVerified: boolean; createdAt: Date; updatedAt: Date; image?: string | null | undefined | undefined; banned: boolean | null | undefined; role?: string | null | undefined; banReason?: string | null | undefined; banExpires?: Date | null | undefined; } | { id: string; name: string; email: string; emailVerified: boolean; createdAt: Date; updatedAt: Date; image?: string | null | undefined | undefined; banned: boolean | null | undefined; role?: string | null | undefined; banReason?: string | null | undefined; banExpires?: Date | null | undefined; } ``` which is not compatible with the `UserWithRole` type because role should either be string or undefined.
Author
Owner

@IamAyaanSk commented on GitHub (May 10, 2025):

@depsimon thanks for commenting, but I was asking something else.
My concern was why the session data returned by useSession hook doesn't contain types that were added by the plugin.
Well this was because I was using useSession in the following way:

import { createAuthClient } from "better-auth/react"
const { useSession } = createAuthClient() 

But instead I should have used my auth-client that had the plugin in the options.

import { authClient } from "@/lib/auth-client" // I should have used my original authClient
const { useSession } = authClient 
@IamAyaanSk commented on GitHub (May 10, 2025): @depsimon thanks for commenting, but I was asking something else. My concern was why the session data returned by useSession hook doesn't contain types that were added by the plugin. Well this was because I was using useSession in the following way: ```ts import { createAuthClient } from "better-auth/react" const { useSession } = createAuthClient() ``` But instead I should have used my auth-client that had the plugin in the options. ```ts import { authClient } from "@/lib/auth-client" // I should have used my original authClient const { useSession } = authClient ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1186