antaeus.keycloak.react 2.1.1 → 2.1.3

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.mjs CHANGED
@@ -1481,6 +1481,8 @@ var DeviceLogin = ({
1481
1481
  };
1482
1482
  function useProtectedFetch(options = {}) {
1483
1483
  const auth = useAuth();
1484
+ const refreshInProgressRef = useRef(false);
1485
+ const { user, isLoading, signinSilent } = auth;
1484
1486
  const {
1485
1487
  baseUrl = "",
1486
1488
  onUnauthorized,
@@ -1499,11 +1501,11 @@ function useProtectedFetch(options = {}) {
1499
1501
  [baseUrl]
1500
1502
  );
1501
1503
  const getAuthHeader = () => {
1502
- if (!auth.user?.access_token) {
1504
+ if (!user?.access_token) {
1503
1505
  return {};
1504
1506
  }
1505
1507
  return {
1506
- Authorization: `Bearer ${auth.user.access_token}`
1508
+ Authorization: `Bearer ${user.access_token}`
1507
1509
  };
1508
1510
  };
1509
1511
  const buildHeaders = useCallback(
@@ -1530,9 +1532,10 @@ function useProtectedFetch(options = {}) {
1530
1532
  });
1531
1533
  if (response.status === 401) {
1532
1534
  onUnauthorized?.();
1533
- if (retryOn401 && auth.user) {
1535
+ if (retryOn401 && user && !refreshInProgressRef.current) {
1536
+ refreshInProgressRef.current = true;
1534
1537
  try {
1535
- await auth.signinSilent();
1538
+ await signinSilent();
1536
1539
  const newHeaders = buildHeaders(init?.headers);
1537
1540
  const retryResponse = await fetch(fullUrl, {
1538
1541
  ...init,
@@ -1541,12 +1544,16 @@ function useProtectedFetch(options = {}) {
1541
1544
  return retryResponse;
1542
1545
  } catch (error) {
1543
1546
  return response;
1547
+ } finally {
1548
+ setTimeout(() => {
1549
+ refreshInProgressRef.current = false;
1550
+ }, 1e3);
1544
1551
  }
1545
1552
  }
1546
1553
  }
1547
1554
  return response;
1548
1555
  },
1549
- [buildUrl, buildHeaders, onUnauthorized, retryOn401, auth]
1556
+ [buildUrl, buildHeaders, onUnauthorized, retryOn401, user, signinSilent]
1550
1557
  );
1551
1558
  const fetchJson = useCallback(
1552
1559
  async (url, init) => {
@@ -1565,7 +1572,7 @@ function useProtectedFetch(options = {}) {
1565
1572
  return {
1566
1573
  fetchJson,
1567
1574
  fetch: protectedFetch,
1568
- isLoading: auth.isLoading
1575
+ isLoading
1569
1576
  };
1570
1577
  }
1571
1578