@swype-org/react-sdk 0.1.266 → 0.1.268

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
@@ -8266,7 +8266,6 @@ function usePasskeyCheckEffect(deps) {
8266
8266
  const existingTransfer = await fetchTransfer(apiBaseUrl, token, persisted.transferId);
8267
8267
  if (cancelled) return;
8268
8268
  if (existingTransfer.status === "COMPLETED") {
8269
- clearMobileFlowState();
8270
8269
  if (persisted.isGuestPreauth && persisted.guestSessionToken) {
8271
8270
  let guestLookup = null;
8272
8271
  try {
@@ -8275,19 +8274,32 @@ function usePasskeyCheckEffect(deps) {
8275
8274
  persisted.guestSessionToken
8276
8275
  );
8277
8276
  } catch {
8278
- dispatch({ type: "MOBILE_RESUME_SUCCESS", transfer: existingTransfer });
8279
- onCompleteRef.current?.(existingTransfer);
8277
+ persistMobileFlowState({
8278
+ deeplinkUri: persisted.deeplinkUri,
8279
+ providerId: persisted.providerId ?? null,
8280
+ isSetup: persisted.isSetup ?? true,
8281
+ guestSessionToken: persisted.guestSessionToken,
8282
+ transferId: persisted.transferId,
8283
+ isGuestPreauth: true,
8284
+ accountId: persisted.accountId,
8285
+ sessionId: persisted.sessionId
8286
+ });
8280
8287
  return;
8281
8288
  }
8282
8289
  if (guestLookup == null) {
8283
- dispatch({
8284
- type: "MOBILE_RESUME_SUCCESS",
8285
- transfer: existingTransfer,
8286
- guestCheckoutResume: { guestSessionToken: persisted.guestSessionToken }
8290
+ persistMobileFlowState({
8291
+ deeplinkUri: persisted.deeplinkUri,
8292
+ providerId: persisted.providerId ?? null,
8293
+ isSetup: persisted.isSetup ?? true,
8294
+ guestSessionToken: persisted.guestSessionToken,
8295
+ transferId: persisted.transferId,
8296
+ isGuestPreauth: true,
8297
+ accountId: persisted.accountId,
8298
+ sessionId: persisted.sessionId
8287
8299
  });
8288
- onCompleteRef.current?.(existingTransfer);
8289
8300
  return;
8290
8301
  }
8302
+ clearMobileFlowState();
8291
8303
  dispatch({
8292
8304
  type: "GUEST_PREAUTH_RESUME_WITH_TRANSFER",
8293
8305
  transfer: existingTransfer,
@@ -8299,6 +8311,7 @@ function usePasskeyCheckEffect(deps) {
8299
8311
  onCompleteRef.current?.(existingTransfer);
8300
8312
  return;
8301
8313
  }
8314
+ clearMobileFlowState();
8302
8315
  dispatch({ type: "MOBILE_RESUME_SUCCESS", transfer: existingTransfer });
8303
8316
  onCompleteRef.current?.(existingTransfer);
8304
8317
  return;
@@ -8586,6 +8599,7 @@ function useProcessingEffect(deps) {
8586
8599
  if (!state.transfer && (polling.transfer.status === "COMPLETED" || polling.transfer.status === "FAILED")) {
8587
8600
  return;
8588
8601
  }
8602
+ if (state.isGuestFlow) return;
8589
8603
  if (polling.transfer.status === "COMPLETED") {
8590
8604
  clearMobileFlowState();
8591
8605
  dispatch({ type: "TRANSFER_COMPLETED", transfer: polling.transfer });
@@ -8600,6 +8614,7 @@ function useProcessingEffect(deps) {
8600
8614
  state.transfer,
8601
8615
  state.loginRequested,
8602
8616
  state.verificationTarget,
8617
+ state.isGuestFlow,
8603
8618
  onComplete,
8604
8619
  dispatch,
8605
8620
  reloadAccounts
@@ -8683,7 +8698,7 @@ function useMobilePollingEffect(deps) {
8683
8698
  if (!state.mobileFlow || !mobileSetupFlowRef.current) return;
8684
8699
  if (!setupAccountIdRef.current) return;
8685
8700
  if (!state.guestPreauthSessionId || !state.isGuestFlow) return;
8686
- const guestToken = state.guestSessionToken;
8701
+ const guestToken = state.guestSessionToken ?? loadMobileFlowState()?.guestSessionToken ?? null;
8687
8702
  const preauthAccountId = state.guestPreauthAccountId;
8688
8703
  const preauthSessionId = state.guestPreauthSessionId;
8689
8704
  let cancelled = false;
@@ -8692,7 +8707,6 @@ function useMobilePollingEffect(deps) {
8692
8707
  const finishMobileAndDispatch = async () => {
8693
8708
  mobileSetupFlowRef.current = false;
8694
8709
  setupAccountIdRef.current = null;
8695
- clearMobileFlowState();
8696
8710
  try {
8697
8711
  await reloadAccounts();
8698
8712
  } catch {
@@ -8703,6 +8717,7 @@ function useMobilePollingEffect(deps) {
8703
8717
  completedGuestPreauth = true;
8704
8718
  cancelled = true;
8705
8719
  await finishMobileAndDispatch();
8720
+ clearMobileFlowState();
8706
8721
  dispatch({ type: "MOBILE_SETUP_COMPLETE" });
8707
8722
  };
8708
8723
  const completeGuestPreauthWithAccount = async (guestLookup) => {
@@ -8730,6 +8745,18 @@ function useMobilePollingEffect(deps) {
8730
8745
  const guestLookup = await fetchGuestAccount(apiBaseUrl, guestToken);
8731
8746
  if (cancelled) return;
8732
8747
  if (guestLookup != null) {
8748
+ if (preauthSessionId) {
8749
+ try {
8750
+ const session = await fetchAuthorizationSession(
8751
+ apiBaseUrl,
8752
+ preauthSessionId
8753
+ );
8754
+ if (cancelled) return;
8755
+ if (session.status !== "AUTHORIZED") return;
8756
+ } catch {
8757
+ return;
8758
+ }
8759
+ }
8733
8760
  await completeGuestPreauthWithAccount(guestLookup);
8734
8761
  }
8735
8762
  } catch {
@@ -8903,63 +8930,97 @@ function guestPreauthRestoreKey(guestToken) {
8903
8930
  function useGuestPreauthMobileRestoreEffect(deps) {
8904
8931
  const { dispatch, apiBaseUrl, privyReady, mobileSetupFlowRef, setupAccountIdRef } = deps;
8905
8932
  const completedRef = react.useRef(false);
8933
+ const pollingCleanupRef = react.useRef(null);
8906
8934
  react.useEffect(() => {
8907
8935
  if (!privyReady) return;
8908
- const persisted = loadMobileFlowState();
8909
- if (!persisted?.isGuestPreauth || !persisted.guestSessionToken) {
8910
- return;
8911
- }
8912
- const guestToken = persisted.guestSessionToken;
8913
- const key = guestPreauthRestoreKey(guestToken);
8914
- if (guestPreauthRestoreAttemptedKeys.has(key)) return;
8915
- guestPreauthRestoreAttemptedKeys.add(key);
8916
- if (completedRef.current) return;
8917
- let cancelled = false;
8918
- const POLL_INTERVAL_MS = 3e3;
8919
- const handleAccountFound = async (guestLookup) => {
8936
+ const tryStart = () => {
8920
8937
  if (completedRef.current) return;
8921
- completedRef.current = true;
8922
- cancelled = true;
8923
- clearMobileFlowState();
8924
- mobileSetupFlowRef.current = false;
8925
- setupAccountIdRef.current = null;
8926
- dispatch({
8927
- type: "GUEST_PREAUTH_DETECTED",
8928
- accountId: guestLookup.accountId,
8929
- sessionId: persisted.sessionId ?? void 0
8930
- });
8931
- dispatch({ type: "REQUEST_LOGIN" });
8932
- dispatch({ type: "MOBILE_SETUP_COMPLETE" });
8933
- };
8934
- const pollGuestAccount = async () => {
8935
- try {
8936
- if (cancelled) return;
8937
- const guestLookup = await fetchGuestAccount(apiBaseUrl, guestToken);
8938
- if (cancelled) return;
8939
- if (guestLookup != null) {
8940
- await handleAccountFound(guestLookup);
8941
- }
8942
- } catch {
8938
+ const persisted = loadMobileFlowState();
8939
+ if (!persisted?.isGuestPreauth || !persisted.guestSessionToken) {
8940
+ return;
8943
8941
  }
8942
+ const guestToken = persisted.guestSessionToken;
8943
+ const key = guestPreauthRestoreKey(guestToken);
8944
+ if (guestPreauthRestoreAttemptedKeys.has(key)) return;
8945
+ guestPreauthRestoreAttemptedKeys.add(key);
8946
+ let cancelled = false;
8947
+ const POLL_INTERVAL_MS = 3e3;
8948
+ mobileSetupFlowRef.current = true;
8949
+ if (persisted.accountId) {
8950
+ setupAccountIdRef.current = persisted.accountId;
8951
+ }
8952
+ const handleAccountFound = async (guestLookup) => {
8953
+ if (completedRef.current) return;
8954
+ completedRef.current = true;
8955
+ cancelled = true;
8956
+ clearMobileFlowState();
8957
+ mobileSetupFlowRef.current = false;
8958
+ setupAccountIdRef.current = null;
8959
+ dispatch({
8960
+ type: "GUEST_PREAUTH_DETECTED",
8961
+ accountId: guestLookup.accountId,
8962
+ sessionId: persisted.sessionId ?? void 0
8963
+ });
8964
+ dispatch({ type: "REQUEST_LOGIN" });
8965
+ dispatch({ type: "MOBILE_SETUP_COMPLETE" });
8966
+ };
8967
+ const pollGuestAccount = async () => {
8968
+ try {
8969
+ if (cancelled) return;
8970
+ const guestLookup = await fetchGuestAccount(apiBaseUrl, guestToken);
8971
+ if (cancelled) return;
8972
+ if (guestLookup != null) {
8973
+ if (persisted.sessionId) {
8974
+ try {
8975
+ const session = await fetchAuthorizationSession(
8976
+ apiBaseUrl,
8977
+ persisted.sessionId
8978
+ );
8979
+ if (cancelled) return;
8980
+ if (session.status !== "AUTHORIZED") return;
8981
+ } catch {
8982
+ return;
8983
+ }
8984
+ }
8985
+ await handleAccountFound(guestLookup);
8986
+ }
8987
+ } catch {
8988
+ }
8989
+ };
8990
+ pollGuestAccount();
8991
+ const intervalId = window.setInterval(pollGuestAccount, POLL_INTERVAL_MS);
8992
+ const handlePollVisibility = () => {
8993
+ if (document.visibilityState === "visible" && !cancelled) void pollGuestAccount();
8994
+ };
8995
+ const handlePollPageShow = (e) => {
8996
+ if (e.persisted && !cancelled) void pollGuestAccount();
8997
+ };
8998
+ document.addEventListener("visibilitychange", handlePollVisibility);
8999
+ window.addEventListener("pageshow", handlePollPageShow);
9000
+ pollingCleanupRef.current = () => {
9001
+ cancelled = true;
9002
+ window.clearInterval(intervalId);
9003
+ document.removeEventListener("visibilitychange", handlePollVisibility);
9004
+ window.removeEventListener("pageshow", handlePollPageShow);
9005
+ if (!completedRef.current) {
9006
+ guestPreauthRestoreAttemptedKeys.delete(key);
9007
+ }
9008
+ pollingCleanupRef.current = null;
9009
+ };
8944
9010
  };
8945
- pollGuestAccount();
8946
- const intervalId = window.setInterval(pollGuestAccount, POLL_INTERVAL_MS);
8947
- const handleVisibility = () => {
8948
- if (document.visibilityState === "visible" && !cancelled) void pollGuestAccount();
9011
+ const onOuterVisibility = () => {
9012
+ if (document.visibilityState === "visible") tryStart();
8949
9013
  };
8950
- const handlePageShow = (e) => {
8951
- if (e.persisted && !cancelled) void pollGuestAccount();
9014
+ const onOuterPageShow = (e) => {
9015
+ if (e.persisted) tryStart();
8952
9016
  };
8953
- document.addEventListener("visibilitychange", handleVisibility);
8954
- window.addEventListener("pageshow", handlePageShow);
9017
+ tryStart();
9018
+ document.addEventListener("visibilitychange", onOuterVisibility);
9019
+ window.addEventListener("pageshow", onOuterPageShow);
8955
9020
  return () => {
8956
- cancelled = true;
8957
- window.clearInterval(intervalId);
8958
- document.removeEventListener("visibilitychange", handleVisibility);
8959
- window.removeEventListener("pageshow", handlePageShow);
8960
- if (!completedRef.current) {
8961
- guestPreauthRestoreAttemptedKeys.delete(key);
8962
- }
9021
+ document.removeEventListener("visibilitychange", onOuterVisibility);
9022
+ window.removeEventListener("pageshow", onOuterPageShow);
9023
+ pollingCleanupRef.current?.();
8963
9024
  };
8964
9025
  }, [privyReady, apiBaseUrl, dispatch, mobileSetupFlowRef, setupAccountIdRef]);
8965
9026
  }