Skip to content
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

Restart login flow if refresh_token is invalid #1135

Merged
merged 8 commits into from
Apr 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions packages/commerce-sdk-react/src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,22 @@ class Auth {
const refreshToken = refreshTokenRegistered || refreshTokenGuest
if (refreshToken) {
try {
return this.queueRequest(
return await this.queueRequest(
() => helpers.refreshAccessToken(this.client, {refreshToken}),
!!refreshTokenGuest
)
} catch {
// If anything bad happens during refresh token flow
// we continue with the PKCE guest user flow.
} catch (error) {
// If the refresh token is invalid, we need to re-login the user
if (error instanceof Error && 'response' in error) {
// commerce-sdk-isomorphic throws a `ResponseError`, but doesn't export the class.
// We can't use `instanceof`, so instead we just check for the `response` property
// and assume it is a fetch Response.
const json = await (error['response'] as Response).json()
if (json.message === 'invalid refresh_token') {
Copy link
Collaborator

Choose a reason for hiding this comment

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

There's not a lot of context for this change in the PR description.
I'm assuming we need to re-implement this because it's a functionality that got lost during the transition from commerce-api folder to commerce-sdk-react.

If that is the case, Do we want to also address the expired token scenario as we did in the commerce-api folder?

const retryErrors = [INVALID_TOKEN, EXPIRED_TOKEN]

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What we did in commerce-api was to consider either for access_token and refresh_token. We handled that slightly different in commerce-react auth, we have checked for access_token expiration first, if it is still available, we don't need to check for refresh_token. In this PR, we only need to fix the bug when refresh_token is invalid

// clean up storage and restart the login flow
this.clearStorage()
}
}
}
}
return this.queueRequest(
Expand Down