[GH-ISSUE #1809] Role Field Not Accessible in Client-Side Session #26250

Closed
opened 2026-04-17 16:44:08 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @Ezeko95 on GitHub (Mar 13, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1809

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

I'm experiencing an issue with accessing user role information in client-side sessions. Despite having roles properly defined in my Prisma schema and a relationship between User and Session models, the role field is not being included in the session object when accessed on the client side.

Environment

NextJS version: 15.2.1
better-auth version: ^1.2.3
Prisma version: ^6.5.0

My schema

enum Role {
  ADMIN
  USER
  MODERATOR
}

model User {
  id            String    @id @default(cuid())
  name          String?
  email         String?   @unique
  emailVerified Boolean?
  image         String?
  password      String?
  age           Int?
  gender        String?
  phone         String?
  role          Role      @default(USER)
  createdAt     DateTime  @default(now())
  updatedAt     DateTime?
  sessions      Session[]
  accounts      Account[]

  @@map("user")
}

My auth-client.ts:

export const authClient = createAuthClient({
  baseURL: process.env.BETTER_AUTH_URL,
  callbacks: {
    session: ({ session, user }: { session: any; user: any }) => {
      return {
        ...session,
        user: {
          ...session.user,
          role: user.role,
        },
      };
    },
  },
});

And my client-component:

type Role = "ADMIN" | "USER" | "MODERATOR";

export default function DashboardPage() {
  const { data: session, isPending, error, refetch } = authClient.useSession();

  const userRole = (session?.user as { role?: Role })?.role;
  console.log("User role:", userRole);

  return <Dashboard />;
}

Current vs. Expected behavior

I expected to get user role from the session and got undefined instead

What version of Better Auth are you using?

^1.2.3

Provide environment information

- Windows 11 pro
- Brave 1.76.74

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

Client, Types

Auth config (if applicable)

import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

export const auth = betterAuth({
  secret: process.env.BETTER_AUTH_SECRET,
  database: prismaAdapter(prisma, {
    provider: "sqlite",
  }),
  emailAndPassword: {
    enabled: true,
  },
  callbacks: {
    session: ({ session, user }: { session: any; user: any }) => {
      return {
        ...session,
        user: {
          ...session.user,
          role: user.role,
        },
      };
    },
  },
});

Additional context

No response

Originally created by @Ezeko95 on GitHub (Mar 13, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1809 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce I'm experiencing an issue with accessing user role information in client-side sessions. Despite having roles properly defined in my Prisma schema and a relationship between User and Session models, the role field is not being included in the session object when accessed on the client side. Environment NextJS version: 15.2.1 better-auth version: ^1.2.3 Prisma version: ^6.5.0 My schema ```prisma enum Role { ADMIN USER MODERATOR } model User { id String @id @default(cuid()) name String? email String? @unique emailVerified Boolean? image String? password String? age Int? gender String? phone String? role Role @default(USER) createdAt DateTime @default(now()) updatedAt DateTime? sessions Session[] accounts Account[] @@map("user") } ``` My auth-client.ts: ```typescript export const authClient = createAuthClient({ baseURL: process.env.BETTER_AUTH_URL, callbacks: { session: ({ session, user }: { session: any; user: any }) => { return { ...session, user: { ...session.user, role: user.role, }, }; }, }, }); ``` And my client-component: ```typescript type Role = "ADMIN" | "USER" | "MODERATOR"; export default function DashboardPage() { const { data: session, isPending, error, refetch } = authClient.useSession(); const userRole = (session?.user as { role?: Role })?.role; console.log("User role:", userRole); return <Dashboard />; } ``` ### Current vs. Expected behavior I expected to get user role from the session and got undefined instead ### What version of Better Auth are you using? ^1.2.3 ### Provide environment information ```bash - Windows 11 pro - Brave 1.76.74 ``` ### Which area(s) are affected? (Select all that apply) Client, Types ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth"; import { prismaAdapter } from "better-auth/adapters/prisma"; import { PrismaClient } from "@prisma/client"; const prisma = new PrismaClient(); export const auth = betterAuth({ secret: process.env.BETTER_AUTH_SECRET, database: prismaAdapter(prisma, { provider: "sqlite", }), emailAndPassword: { enabled: true, }, callbacks: { session: ({ session, user }: { session: any; user: any }) => { return { ...session, user: { ...session.user, role: user.role, }, }; }, }, }); ``` ### Additional context _No response_
GiteaMirror added the lockedbug labels 2026-04-17 16:44:09 -05:00
Author
Owner

@Tarek-Siddique-Nabil commented on GitHub (Mar 16, 2025):

check docs

for role based authorization

and if your problem is solved, please close this issue 😊

<!-- gh-comment-id:2727130905 --> @Tarek-Siddique-Nabil commented on GitHub (Mar 16, 2025): [check docs](https://www.better-auth.com/docs/concepts/session-management#customizing-session-response) [for role based authorization](https://www.better-auth.com/docs/plugins/admin) and if your problem is solved, please close this issue 😊
Author
Owner

@moshetanzer commented on GitHub (Mar 23, 2025):

Hey @Ezeko95,

Issue is that you are trying to add role from user.role but obviously it isn't in user otherwise you wouldn't have to add it.

Assuming you are not using admin plug-in or any other plug it to control role and you just added it to your schema, just make a simple function that queries role from db and add it to user.

Good luck 😊

<!-- gh-comment-id:2746369980 --> @moshetanzer commented on GitHub (Mar 23, 2025): Hey @Ezeko95, Issue is that you are trying to add role from `user.role` but obviously it isn't in `user` otherwise you wouldn't have to add it. Assuming you are not using admin plug-in or any other plug it to control role and you just added it to your schema, just make a simple function that queries role from db and add it to user. Good luck 😊
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#26250