-
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:
Thank you to anyone who can help! :) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi,
Yes!
The data passed in the ...
export default NextAuth({
...
callbacks: {
// Using the `...rest` parameter to be able to narrow down the type based on `trigger`
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
}
}
}) If you need further help, don't hesitate to ask. |
Beta Was this translation helpful? Give feedback.
Hi,
Yes!
The data passed in the
update
method is passed to thesession
argument of thejwt
callback.The
token
argument of thejwt
callback is then passed to theuseSession
method.The documentation says to update it like this: