User object passed into session callback is undefined with successful login. #9438
Replies: 4 comments 6 replies
-
same problem for me |
Beta Was this translation helpful? Give feedback.
-
Have you resolved it? |
Beta Was this translation helpful? Give feedback.
-
Friends -- we seem to have a huge misunderstanding with how the
In other words, the To persist user data, you want to add the user data to the token on initial sign-in only: // https://next-auth.js.org/configuration/callbacks#jwt-callback
async jwt({ token, user: data, account, profile }) {
if (account && account.provider !== 'credentials') {
throw new Error('Unsupported Provider');
}
const isInitialSignIn = account && data;
if (isInitialSignIn) {
const { user, access, refresh } = data;
token.accessToken = access;
token.accessTokenExpires =
getUnixExpirationTimeFromToken(access);
token.refreshToken = refresh;
token.refreshTokenExpires =
getUnixExpirationTimeFromToken(refresh);
token.user = user;
return token;
}
// ... refresh token login below
} The // https://next-auth.js.org/configuration/callbacks#session-callback
async session({ session, user, token }) {
// TODO: Do we need to reject the session here if the tokens are expired?
if (token?.user && token?.accessToken) {
session.user = token.user;
session.accessToken = token.accessToken;
}
return session;
}, If anyone has any feedback on my todo, I'd love to hear your two cents! |
Beta Was this translation helpful? Give feedback.
-
for me it only works after changing the strategy to session: {
strategy: "jwt",
} |
Beta Was this translation helpful? Give feedback.
-
I am using NextAuth and
CredentialsProvider
to login and want to extend the user/session to add a custom field. I overrode myUser
andSession
interfaces. When logging in I am returning a user with the updated model in myauthorize()
func. Myuser
object in thejwt()
callback is valid when I log it, but then seems to immediately run again andundefined
is logged. By the time thesession()
callback is called, theuser
object isundefined
there (I assume because it somehow got set toundefined
in thejwt()
callback).I came to the same conclusion as this post, but it feels like this is incorrect. Why do we have to append the
user
to thetoken
in thejwt()
callback when theuser
is passed into thesession()
callback? I will attach my code/logs below to see if this helps debug.Models
Callbacks
Logs
Beta Was this translation helpful? Give feedback.
All reactions