You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have set up a Next.js project with next-auth as the auth/session manager for my app. The user is being fetched from a mongoDB database, and a session is successfully being returned by useSession() hook in my client side, after being authorized by a bcrypt password comparison.
I'm having problems when trying to add additional data from the database (from the user specifically) into the JWT. My intent is using this additional data for specific auth routes in client side.
When setting up [...nextauth].js callbacks, the async session() callback will not expose the token or user arguments if the async jwt() callback is passed:
if I remove the async jwt() callback, the async session() callback returns the following: {{ name: 'Yeti Dicho', email: '[email protected]', image: undefined }, expires: '2022-04-16T20:12:19.910Z' } session @ session { iat: 1647547459, exp: 1650139459, jti: '96476c59-a501-4129-8a7d-741541039f26' } token @ session undefined user @ session
What is going on? Should I have to work around and do another DB call to get the rest of the necessary information for me to selectively have auth routes on client side?
I have tried the following variations on the sessions() callback: async session({ session, token, user }) { session.user = token.user return session }
Try variations in callback confirguration for two days
Test every single variation found online
Cry for help
Expected behavior
From what I could find online, I hope to pass on to the JWT some data that was fetched and trimmed down from the API, by configuring the token in the JWT callback.
The session should then expose the additional user data on my client.
I read and understand that cookies have a size limit, but even when trying to pass only one additional argument to the session, it is not exposed.
triageUnseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
1 participant
Converted from issue
This discussion was converted from issue #4212 on March 18, 2022 11:52.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Environment
System:
OS: Windows 10 10.0.19044
CPU: (8) x64 Intel(R) Core(TM) i7-8550U CPU @ 1.80GHzz
Memory: 1.45 GB / 7.85 GB
Binaries:
Node: 15.9.0 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.10 - ~\AppData\Roaming\npm\yarn.CMD
npm: 7.19.0 - C:\Program Files\nodejs\npm.CMD
Browsers:
Chrome: 99.0.4844.51 .39)
Edge: Spartan (44.19041.1266.0), Chromium (99.0.1150.39)
Internet Explorer: 11.0.19041.1566
npmPackages:
next: ^12.1.0 => 12.1.0
next-auth: ^4.3.0 => 4.3.0
react: 17.0.2 => 17.0.2
Reproduction URL
https://github.com/dave-torr/ecoandes-gms
Describe the issue
I have set up a Next.js project with next-auth as the auth/session manager for my app. The user is being fetched from a mongoDB database, and a session is successfully being returned by useSession() hook in my client side, after being authorized by a bcrypt password comparison.
I'm having problems when trying to add additional data from the database (from the user specifically) into the JWT. My intent is using this additional data for specific auth routes in client side.
When setting up
[...nextauth].js
callbacks, the async session() callback will not expose the token or user arguments if the async jwt() callback is passed:callbacks:{
async jwt({ token, user }){
if(user){
token.user = {
...user,
company: user.company,
department: user.department,
clientType: user.clientType,
userType: user.userType
}
console.log(token, "@ Token")
return token;
}
},
//reveals object: {user: {name: "Yeti Dicho, email: "[email protected]", image: undefined}, expires: "some expiration date"}
console.log(token, "token @ session")
// logs: undefined token @ session
console.log(user, "user @ session")
// logs: undefined user @ session
return session
},
}
if I remove the async jwt() callback, the async session() callback returns the following:
{{ name: 'Yeti Dicho', email: '[email protected]', image: undefined }, expires: '2022-04-16T20:12:19.910Z' } session @ session
{ iat: 1647547459, exp: 1650139459, jti: '96476c59-a501-4129-8a7d-741541039f26' } token @ session undefined user @ session
What is going on? Should I have to work around and do another DB call to get the rest of the necessary information for me to selectively have auth routes on client side?
I have tried the following variations on the sessions() callback:
async session({ session, token, user }) { session.user = token.user return session }
async session({ session, token, user }) { session.user = user.user return session }
How to reproduce
Expected behavior
From what I could find online, I hope to pass on to the JWT some data that was fetched and trimmed down from the API, by configuring the token in the JWT callback.
The session should then expose the additional user data on my client.
I read and understand that cookies have a size limit, but even when trying to pass only one additional argument to the session, it is not exposed.
Beta Was this translation helpful? Give feedback.
All reactions