update session object from nextAuth | credentialsProvider
Unanswered
Naeemgg posted this in #help-forum
NaeemggOP
can we add some other variables in user object from session ??
as of now I'm getting
I would like the object to be like this
is it possible?
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, 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.
NaeemggOP
So can we update it after user logs in??
I mean user will set his preferred lang after login, so can we update the session object when user is logged in?
I mean user will set his preferred lang after login, so can we update the session object when user is logged in?
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.
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.