Skip to content

Commit

Permalink
fix: ts client If refreshing wait for the existing refreshing promise (
Browse files Browse the repository at this point in the history
  • Loading branch information
jmeistrich authored Dec 17, 2024
1 parent c645591 commit 93102eb
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions node/templates/client/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ export class Core {
*/
refreshToken: this.config?.refreshTokenStore || new InMemoryStore(),

/**
* A promise that resolves when the session is refreshed.
*/
refreshingPromise: undefined as Promise<APIResult<AuthenticationResponse>> | undefined,

/**
* Returns data field set to the list of supported authentication providers and their SSO login URLs.
* Returns error field if an unexpected error occurred.
Expand Down Expand Up @@ -317,10 +322,17 @@ export class Core {
};
}

const authResponse = await this.auth.requestToken({
grant_type: "refresh_token",
refresh_token: refreshToken,
});
// If refreshing already, wait for the existing refreshing promisee
if (!this.auth.refreshingPromise) {
this.auth.refreshingPromise = this.auth.requestToken({
grant_type: "refresh_token",
refresh_token: refreshToken,
});
}

const authResponse = await this.auth.refreshingPromise;

this.auth.refreshingPromise = undefined;

if (authResponse.error) {
return { error: authResponse.error };
Expand Down

0 comments on commit 93102eb

Please sign in to comment.