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 1, 2022
1 parent c9880dc commit f2d8b3b
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 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 @@ -175,8 +176,11 @@ export class RefreshScheme<
cleanObj(endpoint.data)

// Make refresh request
return this.$auth
.request(endpoint, this.options.endpoints.refresh)
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 +190,9 @@ export class RefreshScheme<
this.$auth.callOnError(error, { method: 'refreshToken' })
return Promise.reject(error)
})
.finally(() => {
this.refreshRequest = null
})
}

setUserToken(
Expand Down

0 comments on commit f2d8b3b

Please sign in to comment.