Looking for clarification on session vs token
Unanswered
Himalayan posted this in #help-forum
HimalayanOP
I've got an app using NextAuth and it's working great. I've run into a problem surrounding updating the session, and it's started me questioning my understanding of some key concepts. I've re-read the docs and am still not clear, I'm hoping someone can clarify.
I'm using the
However, now I need to update the session following a user action, I've been reading https://next-auth.js.org/getting-started/client#updating-the-session. Calling
The docs state: "Assuming a strategy: "jwt" is used, the update() method will trigger a jwt callback with the trigger: "update" option. You can use this to update the session object on the server." The example code says:
I don't understand why it is necessary to update the token's name property is helpful here. Clearly it is important so the info in the token is up to date, but how is this used to "update the session object on the server"?
My questions are:
- Am I correct in using
- If so, how can I cause the update I am sending via the
Thank you to anyone who can help! 🙂
I'm using the
jwt and session callbacks. The JWT object, provided to the jwt callback, contains name, email & picture properties. The Session object provided to the session callback, contains a user object, with name, email & image properties. Thus far, I've been using useSession to retrieve user info for display and this has been fine.However, now I need to update the session following a user action, I've been reading https://next-auth.js.org/getting-started/client#updating-the-session. Calling
update causes a POST to /api/auth/session, however, my session object provided by useSession (and therefore my UI) is not being updated. The docs then get a bit weird, and this is the cause of my confusion.The docs state: "Assuming a strategy: "jwt" is used, the update() method will trigger a jwt callback with the trigger: "update" option. You can use this to update the session object on the server." The example code says:
...
jwt({ token, trigger, session }) {
if (trigger === "update" && session?.name) {
// Note, that `session` can be any arbitrary object, remember to validate it!
token.name = session.name
}
return token
}
...I don't understand why it is necessary to update the token's name property is helpful here. Clearly it is important so the info in the token is up to date, but how is this used to "update the session object on the server"?
My questions are:
- Am I correct in using
useSession for retrieving data about the user?- If so, how can I cause the update I am sending via the
update method to be reflected in the session object returned by useSession?Thank you to anyone who can help! 🙂