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