User object passed into session callback is undefined with successful login. #9438
Replies: 5 comments 7 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.
-
|
The type of |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I am using NextAuth and
CredentialsProviderto login and want to extend the user/session to add a custom field. I overrode myUserandSessioninterfaces. When logging in I am returning a user with the updated model in myauthorize()func. Myuserobject in thejwt()callback is valid when I log it, but then seems to immediately run again andundefinedis logged. By the time thesession()callback is called, theuserobject isundefinedthere (I assume because it somehow got set toundefinedin 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
userto thetokenin thejwt()callback when theuseris 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