fix(ui): tokenInMemory not set after refreshing cookie#15928
Conversation
tokenInMemory not set after refreshing cookietokenInMemory not set after refreshing cookie
|
While the PR is small, it’s a bit hard to clearly understand the What and the Why behind it. You do explain it in the PR description, but mostly at a very low level, focusing on the surrounding code and the technical details of the change. What would really help us review and approve the PR faster is a description from a more user-oriented perspective explaining what the actual problem is. For example:
We have an issue template that asks for this type of information, but the associated issue didn’t follow it. If possible, adding a test would also be very helpful. Thanks a lot! |
I tried to use the
I was trying to read the
Use the
Current behavior is that the |
|
It is quite obvious what's going wrong if you look at the code for the The code currently assumes that both endpoints respond with So that is what I fixed in this PR. 😊 Now the code accounts for the difference in shape between the responses. |
…ment - Updated `AuthProvider` to handle both `token` and `refreshedToken` more flexibly. - Modified `AuthDebug` to display the current token and added a button to refresh the auth cookie, ensuring the token remains populated after refresh. - Added an end-to-end test to verify that the token is retained after refreshing the cookie.
GermanJablo
left a comment
There was a problem hiding this comment.
I changed the UserWithToken signature so that it doesn't require a refreshedToken or token because that's not always the case, and I added a test. Thanks.
toHaveText() requires a string or regex argument. Use a refresh counter to synchronize with the async refresh before asserting.
|
🚀 This is included in version v3.81.0 |
…15928) Fixes payloadcms#15926. ### What? In the `AuthContext`, which can be consumed via the `useAuth` hook imported from `@payloadcms/ui`, there is a `token` property which gets set in the internal function `setFullUser`. This function assumes that the `userResponse` argument passed to it always has the token on the property `.token`, when in fact it exists on the property `.refreshedToken` when the function is invoked after the auth cookie has been refreshed. Because of this `useAuth().token` is often nullish when it shouldn't be. ### Why? I want to be able to rely on the `useAuth` hook. Now I can't (or at least not on the `token` property), because there's a bug related to it. ### How? I changed the type for the `userResponse` parameter (`UserWithToken`) to convey that sometimes it's `.token` and sometimes it's `.refreshedToken`. Then I set the `token` property (the `tokenInMemory` state) to either `userResponse.token` or `userResponse.refreshedToken` depending on which property exists on the object (`userResponse`). <!-- Thank you for the PR! Please go through the checklist below and make sure you've completed all the steps. Please review the [CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md) document in this repository if you haven't already. The following items will ensure that your PR is handled as smoothly as possible: - PR Title must follow conventional commits format. For example, `feat: my new feature`, `fix(plugin-seo): my fix`. - Minimal description explained as if explained to someone not immediately familiar with the code. - Provide before/after screenshots or code diffs if applicable. - Link any related issues/discussions from GitHub or Discord. - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Fixes # --> --------- Co-authored-by: German Jablonski <GermanJablo@users.noreply.github.com>
Fixes #15926.
What?
In the
AuthContext, which can be consumed via theuseAuthhook imported from@payloadcms/ui, there is atokenproperty which gets set in the internal functionsetFullUser. This function assumes that theuserResponseargument passed to it always has the token on the property.token, when in fact it exists on the property.refreshedTokenwhen the function is invoked after the auth cookie has been refreshed. Because of thisuseAuth().tokenis often nullish when it shouldn't be.Why?
I want to be able to rely on the
useAuthhook. Now I can't (or at least not on thetokenproperty), because there's a bug related to it.How?
I changed the type for the
userResponseparameter (UserWithToken) to convey that sometimes it's.tokenand sometimes it's.refreshedToken. Then I set thetokenproperty (thetokenInMemorystate) to eitheruserResponse.tokenoruserResponse.refreshedTokendepending on which property exists on the object (userResponse).