Skip to content

feat: refresh token - SFS-2462 - #2936

Merged
eduardoformiga merged 19 commits into
mainfrom
feat/refresh-token
Aug 5, 2025
Merged

eduardoformiga merged 19 commits into
mainfrom
feat/refresh-token

Conversation

@eduardoformiga

@eduardoformiga eduardoformiga commented Jul 8, 2025 •

Copy link
Copy Markdown
Member

What's the purpose of this pull request?

This PR aims to implement the refresh cookie logic to maintain the user logged in.

How does it work?

First, we are using the refreshAfter prop from the session to store the time when we can call the refresh token route again.
After the login, some cookies are set in the response for the production URL: VtexIdclientAutCookie_<account> (access token), and the refresh token cookie in a specific path that we must call in the frontend.

The workflow calls the refresh token request in the frontend (httpOnly), under three conditions, during the validate session mutation:

  1. tokenExistAndIsFirstRefreshTokenRequest— The login has been made, but we need to call the refresh token request at least once to get the refreshAfter for the first time.
  2. tokenNotExistAndRefreshAfterExistAndIsExpired— When the access token expires, the browser clears it, so the access token no longer exists. However, as we have the refreshAfter from the previous request to the refresh token route, we know when to call it again after the refreshAfter time.
  3. tokenExpiredAndRefreshAfterIsNullOrExpired - just a fallback that double-checks the internal expires (claim) prop from the JWT token using the cookie, not the cookie expiration that the browser tasks care.

How to test it?

1 - The refreshToken flag must be true in discovery.config.
2 - You need to ask the identity team (open a support ticket) to enable the refresh token feature and set a time, like 7 days. (b2bfaststoredev account is set to 7 days)
3 - Use a production URL like https://b2bfaststore.vtexfaststore.com/login to login.
4 - After login, the cookies should be set, and you can see a first /webstore request in devtools with the refreshAfter in the response, which will be stored in the session.
5—After 1 day, just double-check if this request is made again and if you will continue logged.

References

@eduardoformiga
eduardoformiga requested a review from a team as a code owner July 8, 2025 23:19
@eduardoformiga
eduardoformiga requested review from gabpaladino and renatamottam and removed request for a team July 8, 2025 23:19
@codesandbox-ci

codesandbox-ci Bot commented Jul 8, 2025 •

Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@eduardoformiga
eduardoformiga removed the request for review from renatamottam August 4, 2025 14:47
Comment on lines +111 to +114
const shouldRefreshToken =
tokenExistAndIsFirstRefreshTokenRequest ||
tokenNotExistAndRefreshAfterExistAndIsExpired ||
tokenExpiredAndRefreshAfterIsNullOrExpired

@eduardoformiga eduardoformiga Aug 4, 2025 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are three conditions for calling the refresh token:

tokenExistAndIsFirstRefreshTokenRequest - login made, but we need to call the refresh token request at least once to get the refreshAfter.
tokenNotExistAndRefreshAfterExistAndIsExpired - when the access token expires, the browser clears it, so the access token doesn't exist anymore, but as we have the refreshAfter from the previous request to the refresh token route, we know when to call it again, after the refreshAfter time.
tokenExpiredAndRefreshAfterIsNullOrExpired- just a fallback that double-checks the internal expires (claim) prop from the JWT token using the cookie, not the cookie expiration that the browser tasks care.

@eduardoformiga eduardoformiga changed the title feat: refresh token feat: refresh token - SFS-2462 Aug 4, 2025
Comment thread packages/core/src/pages/api/graphql.ts Resolved
@eduardoformiga
eduardoformiga merged commit 0fc856f into main Aug 5, 2025
@eduardoformiga
eduardoformiga deleted the feat/refresh-token branch August 5, 2025 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants