From 76ca694c058845fbf17ba06f1e00b9bc6a3787b8 Mon Sep 17 00:00:00 2001 From: shanjayathilaka Date: Sat, 6 Jun 2026 23:54:35 +0530 Subject: [PATCH] Add token refresh logic if the user's access token is expired --- .changeset/hot-wombats-lick.md | 5 ++++ .github/workflows/pr-builder.yml | 2 +- .../helpers/authentication-helper.ts | 24 ++++++++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 .changeset/hot-wombats-lick.md 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; + } } }