Skip to content
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions packages/browser/src/__legacy__/helpers/authentication-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@ export class AuthenticationHelper<T extends MainThreadClientConfig | WebWorkerCl
protected _spaHelper: SPAHelper<T>;
protected _instanceId: number;
protected _isTokenRefreshing: boolean;
protected _refreshAccessTokenPromise?: Promise<User>;

public constructor(authClient: AsgardeoAuthClient<T>, spaHelper: SPAHelper<T>) {
this._authenticationClient = authClient;
this._storageManager = this._authenticationClient.getStorageManager();
this._spaHelper = spaHelper;
this._instanceId = this._authenticationClient.getInstanceId();
this._isTokenRefreshing = false;
this._refreshAccessTokenPromise = undefined;
}

public enableHttpHandler(httpClient: HttpClient): void {
Expand Down Expand Up @@ -174,23 +176,33 @@ export class AuthenticationHelper<T extends MainThreadClientConfig | WebWorkerCl
public async refreshAccessToken(
enableRetrievingSignOutURLFromSession?: (config: SPACustomGrantConfig) => void,
): Promise<User> {
try {
await this._authenticationClient.refreshAccessToken();
const customGrantConfig = await this.getCustomGrantConfigData();
if (customGrantConfig) {
await this.exchangeToken(customGrantConfig, enableRetrievingSignOutURLFromSession);
}
this._spaHelper.refreshAccessTokenAutomatically(this);
if (this._refreshAccessTokenPromise) {
return this._refreshAccessTokenPromise;
}

return this._authenticationClient.getUser();
} catch (error) {
const refreshTokenError: Message<string> = {
type: REFRESH_ACCESS_TOKEN_ERR0R,
};
this._refreshAccessTokenPromise = (async (): Promise<User> => {
try {
await this._authenticationClient.refreshAccessToken();
const customGrantConfig = await this.getCustomGrantConfigData();
if (customGrantConfig) {
await this.exchangeToken(customGrantConfig, enableRetrievingSignOutURLFromSession);
}
this._spaHelper.refreshAccessTokenAutomatically(this);

window.postMessage(refreshTokenError);
return Promise.reject(error);
}
return this._authenticationClient.getUser();
} catch (error) {
const refreshTokenError: Message<string> = {
type: REFRESH_ACCESS_TOKEN_ERR0R,
};

window.postMessage(refreshTokenError);
throw error;
} finally {
this._refreshAccessTokenPromise = undefined;
}
})();

return this._refreshAccessTokenPromise;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

protected async retryFailedRequests(failedRequest: HttpRequestInterface): Promise<HttpResponse> {
Expand Down
Loading