-
-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refresh active JWT tokens #1904
base: master
Are you sure you want to change the base?
Conversation
825430e
to
025c646
Compare
3309f11
to
abed173
Compare
abed173
to
fc05422
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks very close to what we want!
However, two things:
- As you mentioned in chat, this does a db call to fetch the current user which we don't want since this runs on every request
- This only updates the
multi_auth.<id>
token of the current user, but not the other ones
I imagine we can solve both via a new function refreshMultiAuthCookies
that looks at the user list stored in the multi_auth
cookie to loop over every cookie with multi_auth.<id>
and then issues a new JWT given the existing JWT (decodeJWT
+ encodeJWT
).
Since we now always need req
and res
in the jwt callback, I noticed that instead of checking only for req && res
to distinguish new logins and conditionally run setMultiAuthCookies
, we can additionally check for user
.
So this means if user && req && res
is true, we run setMultiAuthCookies
else we run refreshMultiAuthCookies
.
Does this make sense?
I was conflicted on this before, I just thought that if the user is not using their other accounts, might as well let them expire naturally.1 So it was also convenient that setMultiAuthCookies was doing just that! On the other hand refreshing for other logged-in accounts would be really convenient, too... Your solution to this fits perfectly, what do you think?
It does! I thought just that. Footnotes
|
I tested with 5 accounts and got a flawless experience in account switching for the scenarios I picked! Edit: adjusted to #1941 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does refresh all JWTs of other users but I noticed following things:
-
it does not refresh
multi_auth.user-id
which might be important to do since else, the cookie pointer might not get sent to the server anymore 🤔 -
when I switch between users, I get
ERR_HTTP_HEADERS_SENT
in the server logs but the switch seems to work as expected, see video:
2025-03-05.14-23-20.mp4
- this also happens randomly while idle:
2025-03-05.14-50-21.mp4
- I noticed that a signout can lead to
multi_auth.<id>
andmulti_auth.user-id
getting deleted but notmulti_auth
so the code inrefreshMultiAuthCookies
would try to decodeundefined
, see screenshot:
This isn't caused by your code, but we need to handle it until it's fixed. I think we can just check if the token exists before trying to decode it.
I'll debug pages/api/signout.js today. I also noticed that once you switch between users, multi_auth.user-id
expires after the session unlike the other cookies for authentication, including next-auth.session-token
, which expire after 30 days. Maybe that's related to the issues we're seeing with account switching. 🙏
Awesome! Thanks for catching all the stuff I didn't see!
I realized that headers were being sent prematurely which meant that a practical way to solve this would be to group all the cookies to be set and then push all of them together, I didn't get any error after doing this and switching works for me ^^
This is something I didn't encounter though, in my case
After signout:
Nonetheless to avoid every kind of weird scenarios in which an incorrect multi_auth edit happens, now there's a condition to refresh the token if it's actually present.
Another great catch that makes a lot of sense, now it gets refreshed! Super grateful of the work you do with me ^^ |
Description
Addresses #1596
Refreshes the logged-in multi-auth account JWT token together with session
Screenshots
Additional Context
By passing response we trigger setMultiAuthCookies that we used to trigger only on login/sign-up.
We could also specialize the token Set-Cookie to avoid accessing db for user data if we just want the token to be refreshed:
But by refreshing both the multi-auth current user cookie and the user list, we'll probably have less points of failure
Checklist
Are your changes backwards compatible? Please answer below:
todo
On a scale of 1-10 how well and how have you QA'd this change and any features it might affect? Please answer below:
5, normal multi-auth between 3 users, refreshes the multi-auth token together with session, no hiccups
For frontend changes: Tested on mobile, light and dark mode? Please answer below:
n/a
Did you introduce any new environment variables? If so, call them out explicitly here:
todo