Question 💬
I'm using NextAuth with the Credentials Provider for authentication in my Next.js app. I've set up the JWT and session callbacks in my nextauth.js configuration. The JWT callback correctly populates the token object with the user's id and username. However, the session callback doesn't seem to translate that into the session's user object as expected.
Issue:
When I log in, the JWT callback logs a populated token object, but the user object is undefined. Subsequent calls to the JWT callback show the token object with the correct id and username fields. However, the session's user object only has name, email, and image fields, all of which are undefined.
I've tried various solutions, including explicitly setting user fields in the session callback, checking for overwrites, and ensuring the correct structure in the authorize callback, but the issue persists.
Any guidance or suggestions would be greatly appreciated!
How to reproduce ☕️
JWT Callback:
async jwt({ token, user }) { console.log('JWT callback received:', token, user); if (user) { return { ...token, id: user.id, username: user.username, }; } else { console.error('User is undefined:', user); } return token; }
Session Callback:
async session({ session, token }) { if (session && token) { session.user = { id: token.id, username: token.username, }; } else { console.error('Session or token is undefined:', session, token); } return session; }
Logs:
JWT callback received: { sub: '64b74dbc3e53473ebe02f410', id: '64b74dbc3e53473ebe02f410', username: '321', iat: 1693316359, exp: 1693402759, jti: '913eb411-c0cc-4fcc-98f6-ea3b194f2029' } undefined User is undefined: undefined
Session Object:
{ user: { name: undefined, email: undefined, image: undefined }, expires: '2023-09-28T13:39:40.800Z' }
Contributing 🙌🏽
No, I am afraid I cannot help regarding this
Question 💬
I'm using NextAuth with the Credentials Provider for authentication in my Next.js app. I've set up the JWT and session callbacks in my nextauth.js configuration. The JWT callback correctly populates the token object with the user's id and username. However, the session callback doesn't seem to translate that into the session's user object as expected.
Issue:
When I log in, the JWT callback logs a populated token object, but the user object is undefined. Subsequent calls to the JWT callback show the token object with the correct id and username fields. However, the session's user object only has name, email, and image fields, all of which are undefined.
I've tried various solutions, including explicitly setting user fields in the session callback, checking for overwrites, and ensuring the correct structure in the authorize callback, but the issue persists.
Any guidance or suggestions would be greatly appreciated!
How to reproduce ☕️
JWT Callback:
async jwt({ token, user }) { console.log('JWT callback received:', token, user); if (user) { return { ...token, id: user.id, username: user.username, }; } else { console.error('User is undefined:', user); } return token; }Session Callback:
async session({ session, token }) { if (session && token) { session.user = { id: token.id, username: token.username, }; } else { console.error('Session or token is undefined:', session, token); } return session; }Logs:
JWT callback received: { sub: '64b74dbc3e53473ebe02f410', id: '64b74dbc3e53473ebe02f410', username: '321', iat: 1693316359, exp: 1693402759, jti: '913eb411-c0cc-4fcc-98f6-ea3b194f2029' } undefined User is undefined: undefinedSession Object:
{ user: { name: undefined, email: undefined, image: undefined }, expires: '2023-09-28T13:39:40.800Z' }Contributing 🙌🏽
No, I am afraid I cannot help regarding this