diff --git a/node/templates/client/core.ts b/node/templates/client/core.ts index 1df91b31a..5e52f50a2 100644 --- a/node/templates/client/core.ts +++ b/node/templates/client/core.ts @@ -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> | 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. @@ -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 };