Session Update() does not pull from server as stated in the docs
Environment
"next": "13.4.2", "next-auth": "4.22.1", "react": "18.2.0", npm: '9.3.1', node: '18.14.0', MacOs
Reproduction URL
Na
Describe the issue
The docs clearly state when using the update() function that:
"If you are not passing any argument, the session will be reloaded from the server. (This is useful if you want to update the session after a server-side mutation, like updating in the database.)"
When changing the db and subsequently calling this function without any arguments, the session is most definitely NOT pulled from the server.
The callbacks I have configured are:
callbacks: {
async jwt({ token, trigger, user, session: newData }) {
// if an update is requested
if (trigger === "update") {
return { ...token, ...newData };
}
return { ...token, ...user };
},
async session({ session, token }) {
session.user = token as any;
return session;
},
},
I understand with this current configuration there is nothing informing the update trigger to do anything other than return the current token with the updated info. Hence when nothing is passed, nothing new is returned. So why do the docs state it will? It's quite misleading.
Is the expectation to simply add into the JWT callback a condition to identify if the update payload is blank, and if so, fetch the back-end to gain updated user information and append this to the new token?
Seems quite taxing but would be nice to have confirmation this is a "good" workaround for updating the session without passing any parameters, ie when its changed on db not from front-end user-action.
How to reproduce
calling the update() function on any page after a back-end user change will not update the session (from my testing).
Expected behavior
session would be re-fetched and updated accordingly.
We cannot recreate the issue with the provided information. Please add a reproduction in order for us to be able to investigate.
Why was this issue marked with the incomplete label?
To be able to investigate, we need access to a reproduction to identify what triggered the issue. We prefer a link to a public GitHub repository (template), but you can also use a tool like CodeSandbox or StackBlitz.
To make sure the issue is resolved as quickly as possible, please make sure that the reproduction is as minimal as possible. This means that you should remove unnecessary code, files, and dependencies that do not contribute to the issue.
Please test your reproduction against the latest version of NextAuth.js (next-auth@latest) to make sure your issue has not already been fixed.
I added a link, why was it still marked?
Ensure the link is pointing to a codebase that is accessible (e.g. not a private repository). "example.com", "n/a", "will add later", etc. are not acceptable links -- we need to see a public codebase. See the above section for accepted links.
What happens if I don't provide a sufficient minimal reproduction?
Issues with the incomplete label that receives no meaningful activity (e.g. new comments with a reproduction link) are closed after 7 days.
If your issue has not been resolved in that time and it has been closed/locked, please open a new issue with the required reproduction. (It's less likely that we check back on already closed issues.)
I did not open this issue, but it is relevant to me, what can I do to help?
Anyone experiencing the same issue is welcome to provide a minimal reproduction following the above steps. Furthermore, you can upvote the issue using the :+1: reaction on the topmost comment (please do not comment "I have the same issue" without repro steps). Then, we can sort issues by votes to prioritize.
I think my reproduction is good enough, why aren't you looking into it quicker?
We look into every NextAuth.js issue and constantly monitor open issues for new comments.
However, sometimes we might miss one or two. We apologize, and kindly ask you to refrain from tagging core maintainers, as that will usually not result in increased priority.
Upvoting issues to show your interest will help us prioritize and address them as quickly as possible. That said, every issue is important to us, and if an issue gets closed by accident, we encourage you to open a new one linking to the old issue and we will look into it.
Useful Resources
Without publishing my entire infrastructure it will be difficult to publish a reproduction here. The reproduction needs a connection to a db, front-end and back-end. I can however detail more clearly what the situation is:
I have the following call-back config:
callbacks: {
async jwt({ token, trigger, user, session: newData }) {
// if an update is requested
if (trigger === "update") {
return { ...token, ...newData };
}
return { ...token, ...user };
},
async session({ session, token }) {
session.user = token as any;
return session;
},
},
FRONT END: NEXT13 App Dir Page When calling useSession() I can see a session containing all the relevant user meta returned by my authorise config (entire user object). Intended behaviour.
When calling the update() function for the session, I can pass in new user information and it is persisted in the session, returned using useSession(). Intended behaviour.
BACK END: Strapi V4 When a user entry is manually updated. for example changing the name, calling useSession() returns the same stale content in the session. Intended behaviour.
When calling the update() function, without passing in any information, it also returns the same stale data. UNINTENDED. Or at least unintended based on the docs current information that this should pull new information from the server as in this use case.
THE ISSUE: Therefore, when making front-end changes to user information, eg a user-facing profile display, one can send the changes to the back-end, and pass in the changes to the update() function to sync back-end and session information. Rather lengthy, but it works. No complaints.
However, when making a back-end change directly, in my case triggered by stripe webhooks, there is no way to re-update the session from the client side. calling an empty instance of update() does not pull from the server as advertised.
The only work-arounds that seem to pull new server information are to:
- log the user out and back in (not ideal)
- set-up token refreshing (seems unnecessary for something that should be trivial)
- change the default behaviour of the callback so that any call to update without any params triggers a fetch to the db to retrieve new user information.
Ultimately, the documentation is very unclear at how to update the session from the client side after a back-end modification. I am likely misunderstanding something here, so any guidance would be great.
When calling the update() function, without passing in any information, it also returns the same stale data. UNINTENDED. Or at least unintended based on the docs current information that this should pull new information from the server as in this use case.
The update() function does not know that your data has changed.
calling an empty instance of update() does not pull from the server as advertised.
This is incorrect. We do indeed re-fetch the session, whatever was returned from the session callback at the time of the update.
I think your confusion comes from the fact that you are upgrading a third-party (Strapi) API and expect the changes to reflect in the NextAuth.js session. The two aren't coupled (more than you couple them), so you need to re-read the user information from the Strapi API and return the updated result in the jwt callback to persist it.
Ultimately, the documentation is very unclear at how to update the session from the client side after a back-end modification. I am likely misunderstanding something here, so any guidance would be great.
The update() function is intended to update the NextAuth.js session, and nothing more. In your case, the update event seems to happen on the backend and not triggered by the user from the app. We have no way of knowing when this happens from the client.
Closing as this is not a bug.
for some reason update() when no argument is passed it does not execute the update trigger in the callbacks, but if you put something inside it it is executed, this is a bit confusing, but it worked for me
const { data, update } = useSession();
const addFavorite = async () => {
await addFavoriteMutation.mutateAsync(
{ fictitiousProfileId: profileId },
{
onError: () => {
toast({
title: "Erro!",
description: "Error",
variant: "destructive",
});
},
onSuccess: async () => {
toast({
title: "Sucesso!",
description: "Added to favorites",
});
await update({});
},
},
);
};