Repository navigation
feat: refresh token - SFS-2462 - #2936
Conversation
|
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. |
| const shouldRefreshToken = | ||
| tokenExistAndIsFirstRefreshTokenRequest || | ||
| tokenNotExistAndRefreshAfterExistAndIsExpired || | ||
| tokenExpiredAndRefreshAfterIsNullOrExpired |
There was a problem hiding this comment.
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.
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
refreshAfterprop 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:
tokenExistAndIsFirstRefreshTokenRequest— The login has been made, but we need to call the refresh token request at least once to get therefreshAfterfor the first time.tokenNotExistAndRefreshAfterExistAndIsExpired— When the access token expires, the browser clears it, so the access token no longer exists. However, as we have therefreshAfterfrom 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.How to test it?
1 - The
refreshTokenflag must be true indiscovery.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. (
b2bfaststoredevaccount 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
/webstorerequest in devtools with therefreshAfterin 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