Rasmus Eklund
Explore posts from serversDTDrizzle Team
•Created by Rasmus Eklund on 3/31/2024 in #help
Cannot get user role
I'm using Next.js with next auth and the drizzle adapter. I must be doing something wrong because the session users role is undefined.
Interface from T3 to type the session object.
export type UserRole = "user" | "admin";
declare module "next-auth" {
interface Session extends DefaultSession {
user: {
id: string;
// ...other properties
role: UserRole;
} & DefaultSession["user"];
}
interface User {
// ...other properties
role: UserRole;
}
}
My callback:
export const authOptions: NextAuthOptions = {
callbacks: {
session: ({ session, user }) => ({
...session,
user: {
...session.user,
id: user.id,
role: user.role,
},
}),
},
adapter: DrizzleAdapter(db, createTable) as Adapter,
Getting the user:
const session = await getServerAuthSession();
console.log({ sessionUser: session.user });
Output:
{
sessionUser: {
name: 'name',
email: '[email protected]',
image: null,
id: 'uuid',
role: undefined
}
}
Just to make sure the db has user role (same as in the adapter):
export const getUser = async (id: string) => {
return await db
.select()
.from(users)
.where(eq(users.id, id))
.then((res) => res[0] ?? null);
};
const user = await getUser(session.user.id);
console.log({ user });
Output:
{
user: {
id: 'uuid',
name: 'name',
email: '[email protected]',
emailVerified: null,
image: null,
role: 'admin'
}
}
What did i do wrong? Thanks for any help!2 replies
TTCTheo's Typesafe Cult
•Created by Rasmus Eklund on 3/25/2024 in #questions
T3 drizzle error
4 replies