@neog-cloud/neog-api-client 0.1.3 → 0.1.4

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.cjs CHANGED
@@ -1391,6 +1391,8 @@ const getErrorMessage = (error, fallback = "Erro ao processar a requisição") =
1391
1391
  return fallback;
1392
1392
  };
1393
1393
 
1394
+ const USERINFO_ERROR_MESSAGE = "Nao foi possivel carregar dados do usuario";
1395
+ const USERINFO_RETRY_DELAY_MS = 500;
1394
1396
  const resolveExpiresAt = (data) => {
1395
1397
  if (data.expires_at)
1396
1398
  return data.expires_at;
@@ -1469,6 +1471,14 @@ const createAuthProvider = (options) => {
1469
1471
  return null;
1470
1472
  }
1471
1473
  };
1474
+ const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
1475
+ const fetchUserInfoWithRetry = async (accessToken) => {
1476
+ const info = await fetchUserInfo(accessToken);
1477
+ if (info)
1478
+ return info;
1479
+ await wait(USERINFO_RETRY_DELAY_MS);
1480
+ return fetchUserInfo(accessToken);
1481
+ };
1472
1482
  const refreshAccessToken = async () => {
1473
1483
  if (!options.tokenRefreshEndpoint)
1474
1484
  return null;
@@ -1596,9 +1606,16 @@ const createAuthProvider = (options) => {
1596
1606
  let cancelled = false;
1597
1607
  void (async () => {
1598
1608
  var _a, _b;
1599
- const info = await fetchUserInfo(state.token);
1600
- if (!info || cancelled)
1609
+ const info = await fetchUserInfoWithRetry(state.token);
1610
+ if (cancelled)
1611
+ return;
1612
+ if (!info) {
1613
+ handleLogout();
1614
+ if (isMounted.current) {
1615
+ setError(USERINFO_ERROR_MESSAGE);
1616
+ }
1601
1617
  return;
1618
+ }
1602
1619
  const nextState = {
1603
1620
  token: authRef.current.token,
1604
1621
  refreshToken: authRef.current.refreshToken,
@@ -1610,9 +1627,9 @@ const createAuthProvider = (options) => {
1610
1627
  return () => {
1611
1628
  cancelled = true;
1612
1629
  };
1613
- }, [state.token, state.user, syncState]);
1630
+ }, [state.token, state.user, syncState, handleLogout]);
1614
1631
  const login = require$$0.useCallback(async (username, password) => {
1615
- var _a, _b, _c;
1632
+ var _a, _b;
1616
1633
  setLoading(true);
1617
1634
  setError(null);
1618
1635
  try {
@@ -1646,8 +1663,13 @@ const createAuthProvider = (options) => {
1646
1663
  const token = data.access_token;
1647
1664
  const refreshToken = (_b = data.refresh_token) !== null && _b !== void 0 ? _b : null;
1648
1665
  const expiresAt = resolveExpiresAt(data);
1649
- const userInfo = token ? await fetchUserInfo(token) : null;
1650
- const user = (_c = userInfo !== null && userInfo !== void 0 ? userInfo : data.user) !== null && _c !== void 0 ? _c : null;
1666
+ const userInfo = token ? await fetchUserInfoWithRetry(token) : null;
1667
+ if (token && !userInfo) {
1668
+ handleLogout();
1669
+ setError(USERINFO_ERROR_MESSAGE);
1670
+ throw new Error(USERINFO_ERROR_MESSAGE);
1671
+ }
1672
+ const user = userInfo !== null && userInfo !== void 0 ? userInfo : null;
1651
1673
  const nextState = {
1652
1674
  token,
1653
1675
  refreshToken,
@@ -1676,6 +1698,7 @@ const createAuthProvider = (options) => {
1676
1698
  authRequestFormat,
1677
1699
  authGrantType,
1678
1700
  authScope,
1701
+ handleLogout,
1679
1702
  ]);
1680
1703
  const contextValue = require$$0.useMemo(() => {
1681
1704
  var _a, _b;