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!
1 Reply
Sillvva
Sillvva9mo ago
I believe, by default, Drizzle only returns columns included in the table declaration that you pass to a select query. It doesn't do SELECT * FROM table, because it needs to get back the columns in a specific order. Internally, Auth.js (also known as Next Auth) Drizzle adapter passes its own table declarations to the queries. So even if your table declaration has more columns, the Drizzle adapter doesn't know about them. If you want additional columns, you'd need to make an additional query to get them in the session callback and return the session with the updated user.
No description
Want results from more Discord servers?
Add your server