@calimero-network/mero-react 6.0.0 → 6.0.2

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
@@ -328,6 +328,7 @@ function MeroProvider({
328
328
  const [contextId, setContextIdState] = react.useState(() => getContextId());
329
329
  const [contextIdentity, setContextIdentityState] = react.useState(() => getContextIdentity());
330
330
  const meroRef = react.useRef(null);
331
+ const recoveringRef = react.useRef(false);
331
332
  const callbackRef = react.useRef(void 0);
332
333
  if (callbackRef.current === void 0 && isBrowser) {
333
334
  callbackRef.current = meroJs.parseAuthCallback(window.location.href);
@@ -351,12 +352,18 @@ function MeroProvider({
351
352
  async (instance) => {
352
353
  const accessToken = tokenStore.getTokens()?.access_token;
353
354
  if (!accessToken) return false;
354
- try {
355
- const { valid } = await instance.auth.validateToken(accessToken);
356
- return valid;
357
- } catch {
358
- return false;
359
- }
355
+ const validate = async (token) => {
356
+ try {
357
+ const { valid } = await instance.auth.validateToken(token);
358
+ return valid;
359
+ } catch {
360
+ return false;
361
+ }
362
+ };
363
+ if (await validate(accessToken)) return true;
364
+ const rotated = tokenStore.getTokens()?.access_token;
365
+ if (!rotated || rotated === accessToken) return false;
366
+ return validate(rotated);
360
367
  },
361
368
  [tokenStore]
362
369
  );
@@ -483,9 +490,23 @@ function MeroProvider({
483
490
  const onError = (err) => {
484
491
  if (!active) return;
485
492
  setIsOnline(false);
486
- if (err.message.includes("401")) {
487
- logout();
488
- }
493
+ if (!err.message.includes("401")) return;
494
+ if (recoveringRef.current) return;
495
+ recoveringRef.current = true;
496
+ void (async () => {
497
+ try {
498
+ const instance = meroRef.current;
499
+ if (!instance) return;
500
+ if (await checkAuth(instance)) {
501
+ if (active) await instance.events.connect().catch(() => {
502
+ });
503
+ return;
504
+ }
505
+ if (active) logout();
506
+ } finally {
507
+ recoveringRef.current = false;
508
+ }
509
+ })();
489
510
  };
490
511
  sse.on("connect", onConnect);
491
512
  sse.on("error", onError);
@@ -497,7 +518,7 @@ function MeroProvider({
497
518
  sse.off("connect", onConnect);
498
519
  sse.off("error", onError);
499
520
  };
500
- }, [isAuthenticated, mero]);
521
+ }, [isAuthenticated, mero, checkAuth, logout]);
501
522
  const contextValue = react.useMemo(
502
523
  () => ({
503
524
  mero,