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

fix(refresh): prevent duplicating refresh token in parallel requests #1796

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: 14 additions & 3 deletions src/schemes/refresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class RefreshScheme<
{
public refreshToken: RefreshToken
public refreshController: RefreshController
public refreshRequest: Promise<HTTPResponse> | null = null

constructor(
$auth: Auth,
Expand Down Expand Up @@ -174,9 +175,15 @@ export class RefreshScheme<

cleanObj(endpoint.data)

// Make refresh request
return this.$auth
.request(endpoint, this.options.endpoints.refresh)
// When calling refreshTokens() multiple times (parallel axios request)
// Use the same promise as the first refresh request
// or making a refresh request if does not exist
// instead of making parallel refresh request
this.refreshRequest =
this.refreshRequest ||
this.$auth.request(endpoint, this.options.endpoints.refresh)

return this.refreshRequest
.then((response) => {
// Update tokens
this.updateTokens(response, { isRefreshing: true })
Expand All @@ -186,6 +193,10 @@ export class RefreshScheme<
this.$auth.callOnError(error, { method: 'refreshToken' })
return Promise.reject(error)
})
.finally(() => {
// Reset the refresh request
this.refreshRequest = null
})
}

setUserToken(
Expand Down