Skip to content
Draft
Changes from 2 commits
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
46 changes: 29 additions & 17 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 Expand Up @@ -385,9 +397,9 @@ export class AuthenticationHelper<T extends MainThreadClientConfig | WebWorkerCl
})
.catch(async (error: HttpError) => {
if (error?.response?.status === 401 || !error?.response) {
let refreshTokenResponse: TokenResponse | User;
let refreshTokenResponse: User;
try {
refreshTokenResponse = await this._authenticationClient.refreshAccessToken();
refreshTokenResponse = await this.refreshAccessToken();
} catch (refreshError: any) {
if (isHttpHandlerEnabled) {
if (typeof httpErrorCallback === 'function') {
Expand Down
Loading