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
When updating the session cookie in the jwt callback (for example when refreshing an access token) using middleware, the updated session cookie is not available in a server component that gets rendered next, resulting in the access token being refreshed twice.
Update the token in the jwt callback like this: (this simulates a token that is valid for 10 seconds)
if (
!token.refreshedAt ||
addSeconds(token.refreshedAt, 10) < new Date()
) {
console.log("refreshing token....")
token.refreshedAt = new Date()
}
Make sure next-auth is called via a middleware (already the default in next-auth-example).
Now run nom run dev and open the main page.
Expected behavior
The console output refreshing token.... should be printed once, but it's printed multiple times.
This happens because the new session cookie with the refreshed token, created in the middleware, is not passed to the server component.
The text was updated successfully, but these errors were encountered:
nphmuller
added
bug
Something isn't working
triage
Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
labels
Sep 16, 2024
Environment
Reproduction URL
nphmuller/repro-middleware-cookie@d3082fd
Describe the issue
When updating the session cookie in the jwt callback (for example when refreshing an access token) using middleware, the updated session cookie is not available in a server component that gets rendered next, resulting in the access token being refreshed twice.
How to reproduce
nphmuller/repro-middleware-cookie@d3082fd contains a full repro, but this is the gist:
Update the token in the jwt callback like this: (this simulates a token that is valid for 10 seconds)
Make sure next-auth is called via a middleware (already the default in
next-auth-example
).Now run
nom run dev
and open the main page.Expected behavior
The console output
refreshing token....
should be printed once, but it's printed multiple times.This happens because the new session cookie with the refreshed token, created in the middleware, is not passed to the server component.
The text was updated successfully, but these errors were encountered: