Skip to content

Commit

Permalink
fix(refresh): prevent duplicating refresh token in parallel requests
Browse files Browse the repository at this point in the history
  • Loading branch information
trandaison committed Nov 2, 2022
1 parent c9880dc commit 0bcb0b1
Showing 1 changed file with 14 additions and 3 deletions.
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

0 comments on commit 0bcb0b1

Please sign in to comment.