Next.js Discord

Discord Forum

update session object from nextAuth | credentialsProvider

Unanswered
Naeemgg posted this in #help-forum
Open in Discord
can we add some other variables in user object from session ??
as of now I'm getting user: {name,email} in session, I would like to add preferred-lang which will come from database where I'm validating the user.
I would like the object to be like this user:{name,email,preferred-lang}
is it possible?

3 Replies

Satin Angora
Yes, it's possible. Get the user info from the db in the jwt callback, add your extra key/values to the token, return the token -> Get the token in the session callback, set for example session.user.preferred-lang = token.preferred-lang, return the updated session.
Satin Angora
Yes, explained in the docs:
https://next-auth.js.org/getting-started/client#updating-the-session

Updating the session will run the jwt callback again. Fetch user from db -> token -> session callback -> get data from token add to session object -> return updated session.