Extending next-auth user

I'm trying to extend the next-auth user but somehow It's not returning new fields, and I only have this with drizzle with prisma It's working fine.
declare module "next-auth" {
interface Session extends DefaultSession {
user: {
id: string;
// ...other properties
displayName: string;
} & DefaultSession["user"];
}

interface User {
// ...other properties
displayName: string;
}
}

callbacks: {
session: ({ session, user }) => {
console.log(user);
console.log(user.displayName);

return {
...session,
user: {
...session.user,
displayName: user.displayName,
id: user.id,
},
};
},
},
declare module "next-auth" {
interface Session extends DefaultSession {
user: {
id: string;
// ...other properties
displayName: string;
} & DefaultSession["user"];
}

interface User {
// ...other properties
displayName: string;
}
}

callbacks: {
session: ({ session, user }) => {
console.log(user);
console.log(user.displayName);

return {
...session,
user: {
...session.user,
displayName: user.displayName,
id: user.id,
},
};
},
},
Output:
{
id: '965a6ae9-375f-40df-a527-af2e5a31db83',
name: 'thoo0224',
email: 'xxx@gmail.com',
emailVerified: 2024-04-02T14:34:00.000Z,
image: 'https://cdn.discordapp.com/xxx'
}
undefined
{
id: '965a6ae9-375f-40df-a527-af2e5a31db83',
name: 'thoo0224',
email: 'xxx@gmail.com',
emailVerified: 2024-04-02T14:34:00.000Z,
image: 'https://cdn.discordapp.com/xxx'
}
undefined
3 Replies
stoyko
stoyko4mo ago
Are you sure you get the displayName field from the db?
Rasmus Eklund
Rasmus Eklund4mo ago
I had this exact question on the drizzle discord. This is the answer i got: "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." I made an additional db call in the callback to add the data i needed.
stoyko
stoyko4mo 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. This is also a good way to avoid calling unnecessary data, which is good. What I don't understand is 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. It's strange, but hey, as long as it works! You should mark your reply as the solution to your question, so that people in the future know it's solved and can reference it