Skip to content

Commit dfbbb54

Browse files
authored
fix(refresh): prevent duplicating refresh token in parallel requests (#1796)
1 parent c9880dc commit dfbbb54

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/schemes/refresh.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export class RefreshScheme<
5454
{
5555
public refreshToken: RefreshToken
5656
public refreshController: RefreshController
57+
public refreshRequest: Promise<HTTPResponse> | null = null
5758

5859
constructor(
5960
$auth: Auth,
@@ -174,9 +175,15 @@ export class RefreshScheme<
174175

175176
cleanObj(endpoint.data)
176177

177-
// Make refresh request
178-
return this.$auth
179-
.request(endpoint, this.options.endpoints.refresh)
178+
// When calling refreshTokens() multiple times (parallel axios request)
179+
// Use the same promise as the first refresh request
180+
// or making a refresh request if does not exist
181+
// instead of making parallel refresh request
182+
this.refreshRequest =
183+
this.refreshRequest ||
184+
this.$auth.request(endpoint, this.options.endpoints.refresh)
185+
186+
return this.refreshRequest
180187
.then((response) => {
181188
// Update tokens
182189
this.updateTokens(response, { isRefreshing: true })
@@ -186,6 +193,10 @@ export class RefreshScheme<
186193
this.$auth.callOnError(error, { method: 'refreshToken' })
187194
return Promise.reject(error)
188195
})
196+
.finally(() => {
197+
// Reset the refresh request
198+
this.refreshRequest = null
199+
})
189200
}
190201

191202
setUserToken(

0 commit comments

Comments
 (0)