diff --git a/.changeset/hot-wombats-lick.md b/.changeset/hot-wombats-lick.md new file mode 100644 index 000000000..713cb42d7 --- /dev/null +++ b/.changeset/hot-wombats-lick.md @@ -0,0 +1,5 @@ +--- +'@asgardeo/browser': patch +--- + +Add token refresh logic if the user's access token is expired diff --git a/.github/workflows/pr-builder.yml b/.github/workflows/pr-builder.yml index 8284d32b0..e510c338c 100644 --- a/.github/workflows/pr-builder.yml +++ b/.github/workflows/pr-builder.yml @@ -20,7 +20,7 @@ jobs: strategy: matrix: node-version: [lts/*] - pnpm-version: [v10] + pnpm-version: [latest] steps: - name: ⬇️ Checkout id: checkout diff --git a/packages/browser/src/__legacy__/helpers/authentication-helper.ts b/packages/browser/src/__legacy__/helpers/authentication-helper.ts index 867db398b..995f80799 100644 --- a/packages/browser/src/__legacy__/helpers/authentication-helper.ts +++ b/packages/browser/src/__legacy__/helpers/authentication-helper.ts @@ -706,6 +706,28 @@ export class AuthenticationHelper { - return this._authenticationClient.isSignedIn(); + if (await this._authenticationClient.isSignedIn()) { + return true; + } + + // A refresh is already in progress — wait for it to finish then re-check. + if (this._isTokenRefreshing) { + await SPAUtils.until(() => !this._isTokenRefreshing); + + return this._authenticationClient.isSignedIn(); + } + + // Token may be expired — attempt a silent refresh before giving up. + try { + this._isTokenRefreshing = true; + await this.refreshAccessToken(); + this._isTokenRefreshing = false; + + return true; + } catch { + this._isTokenRefreshing = false; + + return false; + } } }