Fix: Ensure Supabase token refresh updates client cookies #2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix: Ensure Supabase token refresh updates client cookies
The
getUser
function in Supabase will refresh the token if needed. However, since this operation is performed server-side, the response must include the updated headers to properly update the cookie stored by the client.This branch includes all the necessary changes to ensure that the cookie is either refreshed or deleted on the client side when required by the server.
Key Change
Previously, the function
getServerClient
only returned the Supabase client, but it did not expose the updated headers needed for the client to update its cookies.Before:
After (Fix):
The updated function now returns both the Supabase client and the headers, ensuring that any changes to the cookies are properly passed back in the response.
Example Usage in a React Router v7 Loader/Action
To ensure that supabase updates the client-side cookies correctly, you should include the returned headers in your loader or action response.
This ensures that if the user's token is refreshed or invalid, the appropriate response headers are sent back to update or remove the cookies on the client.
How to Verify Token Refresh
To verify that the token is correctly refreshed, you can open the developer console in your browser, retrieve the token from your cookies or local storage, and copy it into jwt.io. Check the
exp
orexpires_at
field in the payload. When the expiration time is reached, refresh the page on your site and verify that a new token is generated.This fix ensures that when the server refreshes the token, the client-side cookies stay in sync.
Let me know if any further improvements are needed! 🚀