@block-auth.io/core 0.9.0 → 0.9.1

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
@@ -2438,6 +2438,9 @@ function createAuthRuntime(client, callbacks = {}) {
2438
2438
  const provider = client.provider;
2439
2439
  const hookState = {
2440
2440
  isUnsignTimeoutSet: false,
2441
+ // When the current L1 signature expires (epoch ms), set with the unsign
2442
+ // timeout; 0 when unknown. Read only while isUnsignTimeoutSet holds.
2443
+ signedExpiresAtMs: 0,
2441
2444
  isSigned: false,
2442
2445
  isSigning: false,
2443
2446
  isLoggingOut: false,
@@ -2691,17 +2694,35 @@ function createAuthRuntime(client, callbacks = {}) {
2691
2694
  const currentDate = new Date(getUTCDateString(/* @__PURE__ */ new Date()));
2692
2695
  const signedExpiredAt = new Date(signedExpiredAtDateString);
2693
2696
  const timeDifference = signedExpiredAt.getTime() - currentDate.getTime();
2697
+ hookState.signedExpiresAtMs = Number.isFinite(timeDifference) ? Date.now() + timeDifference : 0;
2694
2698
  unsignTimeout = setTimeout(() => {
2695
2699
  emitSdkSign(wsc().accessToken, did);
2696
2700
  }, timeDifference);
2697
2701
  };
2702
+ const rearmUnsignTimeout = (signedExpiredAtDateString, did = "") => {
2703
+ const currentDate = new Date(getUTCDateString(/* @__PURE__ */ new Date()));
2704
+ const timeDifference = new Date(signedExpiredAtDateString).getTime() - currentDate.getTime();
2705
+ if (!Number.isFinite(timeDifference)) return;
2706
+ const expiresAtMs = Date.now() + timeDifference;
2707
+ if (Math.abs(expiresAtMs - hookState.signedExpiresAtMs) <= 1e3) return;
2708
+ if (unsignTimeout) {
2709
+ clearTimeout(unsignTimeout);
2710
+ unsignTimeout = null;
2711
+ }
2712
+ setUnsignTimeout(signedExpiredAtDateString, did);
2713
+ };
2698
2714
  const signinUnsigned = async () => {
2699
2715
  setIsSignedFirstCheck(true);
2700
2716
  if (hookState.isSigning) return;
2701
2717
  if (!hookState.isSigned) {
2702
2718
  await _onError();
2719
+ return;
2720
+ }
2721
+ if (signatureExpired()) {
2722
+ await _onError();
2703
2723
  }
2704
2724
  };
2725
+ const signatureExpired = () => hookState.isUnsignTimeoutSet && hookState.signedExpiresAtMs > 0 && Date.now() >= hookState.signedExpiresAtMs;
2705
2726
  const onProviderSignup = (data) => {
2706
2727
  const { success, message } = data;
2707
2728
  if (!success) {
@@ -2846,6 +2867,9 @@ function createAuthRuntime(client, callbacks = {}) {
2846
2867
  setToken(_token);
2847
2868
  setIsSigned(success);
2848
2869
  hookState.isSigned = success;
2870
+ if (success && hookState.isUnsignTimeoutSet && message?.signedExpiredAt) {
2871
+ rearmUnsignTimeout(message.signedExpiredAt, _did);
2872
+ }
2849
2873
  if (success && !hookState.isUnsignTimeoutSet) {
2850
2874
  if (typeof localStorage !== "undefined") {
2851
2875
  localStorage.removeItem("blockauth-logged-out");