Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions .changeset/quick-camels-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@asgardeo/browser': patch
'@asgardeo/react': patch
---

Fix the race condition in token refresh
21 changes: 18 additions & 3 deletions packages/browser/src/__legacy__/helpers/spa-helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* Copyright (c) 2020-2026, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
Expand All @@ -24,6 +24,7 @@ import {MainThreadClientConfig, WebWorkerClientConfig} from '../models/client-co
export class SPAHelper<T extends MainThreadClientConfig | WebWorkerClientConfig> {
private _authenticationClient: AsgardeoAuthClient<T>;
private _storageManager: StorageManager<T>;
private _isTokenRefreshLoading: boolean = false;

public constructor(authClient: AsgardeoAuthClient<T>) {
this._authenticationClient = authClient;
Expand Down Expand Up @@ -52,12 +53,26 @@ export class SPAHelper<T extends MainThreadClientConfig | WebWorkerClientConfig>
const timeUntilRefresh = absoluteExpiryTime - Date.now() - TOKEN_REFRESH_BUFFER_MS;

if (timeUntilRefresh <= 0) {
await authenticationHelper.refreshAccessToken();
if (this._isTokenRefreshLoading) return;

this._isTokenRefreshLoading = true;
try {
await authenticationHelper.refreshAccessToken();
} finally {
this._isTokenRefreshLoading = false;
}
return;
}

const timer = setTimeout(async () => {
await authenticationHelper.refreshAccessToken();
if (this._isTokenRefreshLoading) return;

this._isTokenRefreshLoading = true;
try {
await authenticationHelper.refreshAccessToken();
} finally {
this._isTokenRefreshLoading = false;
}
}, timeUntilRefresh);

await this._storageManager.setTemporaryDataParameter(
Expand Down
Loading