@thunderid/browser 1.14.0 → 1.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -693,6 +693,12 @@ var SPAUtils_default = SPAUtils;
693
693
 
694
694
  //#endregion
695
695
  //#region src/utils/AuthenticationHelper.ts
696
+ /** Methods that change nothing on the server, so sending one twice is harmless. */
697
+ const RETRY_SAFE_HTTP_METHODS = [
698
+ "GET",
699
+ "HEAD",
700
+ "OPTIONS"
701
+ ];
696
702
  /**
697
703
  * Browser-level authentication helper that orchestrates HTTP requests with token attachment,
698
704
  * automatic token refresh, and the silent sign-in flow.
@@ -808,6 +814,21 @@ var AuthenticationHelper = class {
808
814
  return Promise.reject(error);
809
815
  }
810
816
  }
817
+ /**
818
+ * Whether a failed request should be sent again after a token refresh: any method on a 401, and
819
+ * only a safe method when there is no response, since the server may have acted on it. A request the
820
+ * caller aborted is never sent again.
821
+ *
822
+ * @param error - The error the request failed with.
823
+ * @param requestConfig - The failed request's configuration.
824
+ * @returns `true` if the request should be sent again after a token refresh.
825
+ */
826
+ shouldRetryAfterRefresh(error, requestConfig) {
827
+ if (error?.code === "ABORT_ERROR" || error?.name === "AbortError") return false;
828
+ if (error?.response?.status === 401) return true;
829
+ if (error?.response) return false;
830
+ return RETRY_SAFE_HTTP_METHODS.includes((requestConfig?.method ?? "GET").toUpperCase());
831
+ }
811
832
  async retryFailedRequests(failedRequest) {
812
833
  const { httpClient, requestConfig, isHttpHandlerEnabled, httpErrorCallback, httpFinishCallback } = failedRequest;
813
834
  await SPAUtils_default.until(() => !this._isTokenRefreshing);
@@ -823,7 +844,7 @@ var AuthenticationHelper = class {
823
844
  }
824
845
  /**
825
846
  * Sends an HTTP request via the provided client, automatically attaching the token,
826
- * and retries once after a token refresh on a 401 response.
847
+ * and retries once after a token refresh when `shouldRetryAfterRefresh` allows it.
827
848
  *
828
849
  * @param httpClient - The HTTP client to use.
829
850
  * @param requestConfig - The request configuration.
@@ -837,7 +858,7 @@ var AuthenticationHelper = class {
837
858
  return httpClient.request(requestConfig).then((response) => {
838
859
  return Promise.resolve(response);
839
860
  }).catch(async (error) => {
840
- if (error?.response?.status === 401 || !error?.response) {
861
+ if (this.shouldRetryAfterRefresh(error, requestConfig)) {
841
862
  if (this._isTokenRefreshing) return this.retryFailedRequests({
842
863
  enableRetrievingSignOutURLFromSession,
843
864
  httpClient,