Nextjs API auth.api.getSession session.user.role undefined

I try to get the user Role from session , it become undefined. DB already have a admin account with role = admin.
// one of the API Check authentication
const session = await auth.api.getSession({
headers: request.headers,
});

if (!session?.user) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
console.log("session", session);
const userRole = session.user.role; <---- aways undefined
if (userRole !== "admin") {
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
}

//console.log
session {
session: {
...
userId: 'admin',
impersonatedBy: undefined,
id: 'B3E53U7JbzzuY2r1baDBV89imYnMjRMS'
},
user: {
name: 'Admin User',
email: 'admin@admin.com',
emailVerified: true,
image: 'https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?w=400',
createdAt: 2025-07-05T19:45:02.505Z,
updatedAt: 2025-07-05T19:45:02.505Z,
role: undefined,
banned: undefined,
banReason: undefined,
banExpires: undefined,
id: 'admin'
}
}


//Auth Config
export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: "postgresql",
}),
plugins: [
nextCookies(),
apiKey({
enableMetadata: true,
}),
openAPI(),
admin({
ac, // Access control system
roles: {
user: user,
admin: adminRole,
},
defaultRole: "user", // Default role for new registrations
adminRoles: ["admin"], // Only admin role has full admin privileges
defaultBanReason: "Violation of platform terms and conditions",
impersonationSessionDuration: 60 * 60 * 2, // 2 hours for admin impersonation
bannedUserMessage:
"Your account has been suspended. Please contact support for assistance.",
}),
],
},
emailAndPassword: {
enabled: true,
}
});
// one of the API Check authentication
const session = await auth.api.getSession({
headers: request.headers,
});

if (!session?.user) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
console.log("session", session);
const userRole = session.user.role; <---- aways undefined
if (userRole !== "admin") {
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
}

//console.log
session {
session: {
...
userId: 'admin',
impersonatedBy: undefined,
id: 'B3E53U7JbzzuY2r1baDBV89imYnMjRMS'
},
user: {
name: 'Admin User',
email: 'admin@admin.com',
emailVerified: true,
image: 'https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?w=400',
createdAt: 2025-07-05T19:45:02.505Z,
updatedAt: 2025-07-05T19:45:02.505Z,
role: undefined,
banned: undefined,
banReason: undefined,
banExpires: undefined,
id: 'admin'
}
}


//Auth Config
export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: "postgresql",
}),
plugins: [
nextCookies(),
apiKey({
enableMetadata: true,
}),
openAPI(),
admin({
ac, // Access control system
roles: {
user: user,
admin: adminRole,
},
defaultRole: "user", // Default role for new registrations
adminRoles: ["admin"], // Only admin role has full admin privileges
defaultBanReason: "Violation of platform terms and conditions",
impersonationSessionDuration: 60 * 60 * 2, // 2 hours for admin impersonation
bannedUserMessage:
"Your account has been suspended. Please contact support for assistance.",
}),
],
},
emailAndPassword: {
enabled: true,
}
});
Solution:
can u try to put nextCookies plugin to the bottom
Jump to solution
3 Replies
kelvin_kwong
kelvin_kwongOP3mo ago
No description
Solution
nikatune
nikatune3mo ago
can u try to put nextCookies plugin to the bottom
kelvin_kwong
kelvin_kwongOP3mo ago
LOL it work , Thanks

Did you find this page helpful?