Typescript not seeing role as a valid attribute

I'm trying to check if a user is admin in order to access admin only pages. To do this I have this code, it works fine.
const session = await auth.api.getSession({
headers: await headers(),
});
const isAdmin = session?.user?.role === "admin";
if (!isAdmin) {
return "Not admin"
}
const session = await auth.api.getSession({
headers: await headers(),
});
const isAdmin = session?.user?.role === "admin";
if (!isAdmin) {
return "Not admin"
}
But typescript says role dosen't exist Property 'role' does not exist on type '{ id: string; name: string; email: string; emailVerified: boolean; createdAt: Date; updatedAt: Date; image?: string | null | undefined; }'.ts(2339) I know the role comes from better-auth, it's the default admin role. Why can't typescript see it? auth.ts https://pastebin.com/J1TV7T8S
Pastebin
import { betterAuth } from "better-auth";import { nextCookies } fro...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
2 Replies
aswnss
aswnss4w ago
I dont think better auth has default role , we need to extend the default session to add role did you add that in the auth.ts file and for typescript you need add the additional role in the authClient too
Vimes
VimesOP4w ago
I probably just forgot that I added it them, weird that I see it in the user object in console log I tried something like this, in auth.ts
user: {
deleteUser: {
enabled: true,
},
additionalFields: {
role: {
type: "string",
input: false,
},
},
},
user: {
deleteUser: {
enabled: true,
},
additionalFields: {
role: {
type: "string",
input: false,
},
},
},
in authClient.ts
import { createAuthClient } from "better-auth/react";
import {
adminClient,
organizationClient,
inferAdditionalFields,
} from "better-auth/client/plugins";
import type { auth } from "@/lib/auth";
export const authClient = createAuthClient({
plugins: [
adminClient(),
inferAdditionalFields<typeof auth>(),
organizationClient(),
],
});
type Session = typeof authClient.$Infer.Session;
import { createAuthClient } from "better-auth/react";
import {
adminClient,
organizationClient,
inferAdditionalFields,
} from "better-auth/client/plugins";
import type { auth } from "@/lib/auth";
export const authClient = createAuthClient({
plugins: [
adminClient(),
inferAdditionalFields<typeof auth>(),
organizationClient(),
],
});
type Session = typeof authClient.$Infer.Session;
But I still get the same TS error I also tried adding it manually to authClient, but still get the same error
export const authClient = createAuthClient({
plugins: [
adminClient(),
organizationClient(),

inferAdditionalFields({
user: {
role: {
type: "string",
},
},
}),
],
});
type Session = typeof authClient.$Infer.Session;
export const authClient = createAuthClient({
plugins: [
adminClient(),
organizationClient(),

inferAdditionalFields({
user: {
role: {
type: "string",
},
},
}),
],
});
type Session = typeof authClient.$Infer.Session;

Did you find this page helpful?