Next.js Discord

Discord Forum

Passing Full User Info from Authorization to JWT/Session Callback

Answered
Saltwater Crocodile posted this in #help-forum
Open in Discord
Saltwater CrocodileOP
How can I ensure that all user information, including created_at and updated_at, is passed from the authorization step to the JWT/session callback?

In my database, each user record contains fields such as id, password, created_at, and updated_at. However, when I return the user information during authorization, and then log it in the JWT/session callback, I notice that the created_at and updated_at fields are missing. How can I ensure that these fields are included? Do I need to make an additional database call?
Answered by Saltwater Crocodile
@Scale parasitoid Solved!

I was calling the session callback inside auth.config.ts instead of auth.ts!

thats why i was getting the errors!
View full answer

12 Replies

Scale parasitoid
@Saltwater Crocodile just had this case today when using credential provider. What I do is I get the user again from database, and assign the properties I want to to the user
callbacks: {
async session({ session, token }) {

const {data: user, error} = await supabase
.from('users')
.select()
.eq('email', token.email)


session.accessToken = token.accessToken;
session.user.id = token.id;
session.user.role = user[0].role;

return session;
}
},
Saltwater CrocodileOP
Yeah, im trying to avoid that if possible haha

But thanks i'll have to do that if there is no other option
Saltwater CrocodileOP
are you able to make calls within session? mine is giving errors idk why

@Scale parasitoid
Scale parasitoid
For me it works
But I'm getting the user with its email
What does token.sub contain?
Saltwater CrocodileOP
token.sub contains the id of the user, since it's a primary key it's faster to fetch this way

still didnt figure out the issue, but its not related to my query, probably some includes in that file, its a mariadb library error, i get even when i dont call the fetch.. @Scale parasitoid
Scale parasitoid
Can you console.log the token.sub and see what you get?
@Saltwater Crocodile
Saltwater CrocodileOP
yeah i get 4 which is the id from that user

that is always the user id

https://github.com/nextauthjs/next-auth/discussions/6695
Saltwater CrocodileOP
@Scale parasitoid
Saltwater CrocodileOP
@Scale parasitoid Solved!

I was calling the session callback inside auth.config.ts instead of auth.ts!

thats why i was getting the errors!
Answer