@swype-org/react-sdk 0.1.264 → 0.1.267

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
@@ -2418,7 +2418,8 @@ function applyAction(state, action) {
2418
2418
  case "MOBILE_SETUP_COMPLETE":
2419
2419
  return {
2420
2420
  ...state,
2421
- transfer: action.transfer ?? state.transfer,
2421
+ // Do not use `??`: explicit `null` must clear the transfer (guest preauth → login / claim).
2422
+ transfer: action.transfer !== void 0 ? action.transfer : state.transfer,
2422
2423
  error: null,
2423
2424
  mobileFlow: false,
2424
2425
  deeplinkUri: null
@@ -8265,7 +8266,6 @@ function usePasskeyCheckEffect(deps) {
8265
8266
  const existingTransfer = await fetchTransfer(apiBaseUrl, token, persisted.transferId);
8266
8267
  if (cancelled) return;
8267
8268
  if (existingTransfer.status === "COMPLETED") {
8268
- clearMobileFlowState();
8269
8269
  if (persisted.isGuestPreauth && persisted.guestSessionToken) {
8270
8270
  let guestLookup = null;
8271
8271
  try {
@@ -8274,19 +8274,32 @@ function usePasskeyCheckEffect(deps) {
8274
8274
  persisted.guestSessionToken
8275
8275
  );
8276
8276
  } catch {
8277
- dispatch({ type: "MOBILE_RESUME_SUCCESS", transfer: existingTransfer });
8278
- 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
+ });
8279
8287
  return;
8280
8288
  }
8281
8289
  if (guestLookup == null) {
8282
- dispatch({
8283
- type: "MOBILE_RESUME_SUCCESS",
8284
- transfer: existingTransfer,
8285
- 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
8286
8299
  });
8287
- onCompleteRef.current?.(existingTransfer);
8288
8300
  return;
8289
8301
  }
8302
+ clearMobileFlowState();
8290
8303
  dispatch({
8291
8304
  type: "GUEST_PREAUTH_RESUME_WITH_TRANSFER",
8292
8305
  transfer: existingTransfer,
@@ -8298,6 +8311,7 @@ function usePasskeyCheckEffect(deps) {
8298
8311
  onCompleteRef.current?.(existingTransfer);
8299
8312
  return;
8300
8313
  }
8314
+ clearMobileFlowState();
8301
8315
  dispatch({ type: "MOBILE_RESUME_SUCCESS", transfer: existingTransfer });
8302
8316
  onCompleteRef.current?.(existingTransfer);
8303
8317
  return;
@@ -8585,6 +8599,7 @@ function useProcessingEffect(deps) {
8585
8599
  if (!state.transfer && (polling.transfer.status === "COMPLETED" || polling.transfer.status === "FAILED")) {
8586
8600
  return;
8587
8601
  }
8602
+ if (state.isGuestFlow) return;
8588
8603
  if (polling.transfer.status === "COMPLETED") {
8589
8604
  clearMobileFlowState();
8590
8605
  dispatch({ type: "TRANSFER_COMPLETED", transfer: polling.transfer });
@@ -8599,6 +8614,7 @@ function useProcessingEffect(deps) {
8599
8614
  state.transfer,
8600
8615
  state.loginRequested,
8601
8616
  state.verificationTarget,
8617
+ state.isGuestFlow,
8602
8618
  onComplete,
8603
8619
  dispatch,
8604
8620
  reloadAccounts
@@ -8653,11 +8669,9 @@ function useMobilePollingEffect(deps) {
8653
8669
  reloadAccounts,
8654
8670
  apiBaseUrl
8655
8671
  } = deps;
8656
- const { getAccessToken, authenticated: privyAuthenticated } = reactAuth.usePrivy();
8672
+ const { getAccessToken } = reactAuth.usePrivy();
8657
8673
  const getAccessTokenRef = react.useRef(getAccessToken);
8658
8674
  getAccessTokenRef.current = getAccessToken;
8659
- const privyAuthenticatedRef = react.useRef(privyAuthenticated);
8660
- privyAuthenticatedRef.current = privyAuthenticated;
8661
8675
  const handleAuthorizedMobileReturnRef = react.useRef(deps.handleAuthorizedMobileReturn);
8662
8676
  handleAuthorizedMobileReturnRef.current = deps.handleAuthorizedMobileReturn;
8663
8677
  react.useEffect(() => {
@@ -8684,7 +8698,7 @@ function useMobilePollingEffect(deps) {
8684
8698
  if (!state.mobileFlow || !mobileSetupFlowRef.current) return;
8685
8699
  if (!setupAccountIdRef.current) return;
8686
8700
  if (!state.guestPreauthSessionId || !state.isGuestFlow) return;
8687
- const guestToken = state.guestSessionToken;
8701
+ const guestToken = state.guestSessionToken ?? loadMobileFlowState()?.guestSessionToken ?? null;
8688
8702
  const preauthAccountId = state.guestPreauthAccountId;
8689
8703
  const preauthSessionId = state.guestPreauthSessionId;
8690
8704
  let cancelled = false;
@@ -8718,9 +8732,7 @@ function useMobilePollingEffect(deps) {
8718
8732
  sessionId: preauthSessionId ?? void 0
8719
8733
  });
8720
8734
  }
8721
- if (!privyAuthenticatedRef.current) {
8722
- dispatch({ type: "REQUEST_LOGIN" });
8723
- }
8735
+ dispatch({ type: "REQUEST_LOGIN" });
8724
8736
  dispatch({ type: "MOBILE_SETUP_COMPLETE" });
8725
8737
  };
8726
8738
  const pollGuestAccount = async () => {
@@ -8900,88 +8912,93 @@ function useMobilePollingEffect(deps) {
8900
8912
  ]);
8901
8913
  }
8902
8914
  var guestPreauthRestoreAttemptedKeys = /* @__PURE__ */ new Set();
8903
- function guestPreauthRestoreKey(sessionId, transferId) {
8904
- return `${sessionId}:${transferId}`;
8915
+ function guestPreauthRestoreKey(guestToken) {
8916
+ return guestToken;
8905
8917
  }
8906
8918
  function useGuestPreauthMobileRestoreEffect(deps) {
8907
- const {
8908
- dispatch,
8909
- apiBaseUrl,
8910
- privyReady,
8911
- authenticated,
8912
- mobileSetupFlowRef,
8913
- setupAccountIdRef
8914
- } = deps;
8919
+ const { dispatch, apiBaseUrl, privyReady, mobileSetupFlowRef, setupAccountIdRef } = deps;
8920
+ const completedRef = react.useRef(false);
8921
+ const pollingCleanupRef = react.useRef(null);
8915
8922
  react.useEffect(() => {
8916
- if (!privyReady || authenticated) return;
8917
- const persisted = loadMobileFlowState();
8918
- if (!persisted?.isGuestPreauth || !persisted.transferId || !persisted.guestSessionToken || !persisted.sessionId || !persisted.deeplinkUri || persisted.accountId == null) {
8919
- return;
8920
- }
8921
- const key = guestPreauthRestoreKey(persisted.sessionId, persisted.transferId);
8922
- if (guestPreauthRestoreAttemptedKeys.has(key)) return;
8923
- guestPreauthRestoreAttemptedKeys.add(key);
8924
- let cancelled = false;
8925
- void (async () => {
8926
- try {
8927
- const [transfer, session] = await Promise.all([
8928
- getGuestTransfer(apiBaseUrl, persisted.transferId, persisted.guestSessionToken),
8929
- fetchAuthorizationSession(apiBaseUrl, persisted.sessionId)
8930
- ]);
8931
- if (cancelled) return;
8932
- const mobileOpen = session.status !== "AUTHORIZED";
8933
- let guestLookup = null;
8934
- if (!mobileOpen) {
8935
- try {
8936
- guestLookup = await fetchGuestAccount(
8937
- apiBaseUrl,
8938
- persisted.guestSessionToken
8939
- );
8940
- } catch {
8941
- dispatch({
8942
- type: "MOBILE_RESUME_SUCCESS",
8943
- transfer,
8944
- guestCheckoutResume: { guestSessionToken: persisted.guestSessionToken }
8945
- });
8946
- clearMobileFlowState();
8947
- return;
8948
- }
8949
- if (guestLookup == null) {
8950
- dispatch({
8951
- type: "MOBILE_RESUME_SUCCESS",
8952
- transfer,
8953
- guestCheckoutResume: { guestSessionToken: persisted.guestSessionToken }
8954
- });
8955
- clearMobileFlowState();
8956
- return;
8957
- }
8958
- }
8959
- const guestPreauthAccountId = !mobileOpen && guestLookup != null ? guestLookup.accountId : persisted.accountId;
8923
+ if (!privyReady) return;
8924
+ const tryStart = () => {
8925
+ if (completedRef.current) return;
8926
+ const persisted = loadMobileFlowState();
8927
+ if (!persisted?.isGuestPreauth || !persisted.guestSessionToken) {
8928
+ return;
8929
+ }
8930
+ const guestToken = persisted.guestSessionToken;
8931
+ const key = guestPreauthRestoreKey(guestToken);
8932
+ if (guestPreauthRestoreAttemptedKeys.has(key)) return;
8933
+ guestPreauthRestoreAttemptedKeys.add(key);
8934
+ let cancelled = false;
8935
+ const POLL_INTERVAL_MS = 3e3;
8936
+ mobileSetupFlowRef.current = true;
8937
+ if (persisted.accountId) {
8938
+ setupAccountIdRef.current = persisted.accountId;
8939
+ }
8940
+ const handleAccountFound = async (guestLookup) => {
8941
+ if (completedRef.current) return;
8942
+ completedRef.current = true;
8943
+ cancelled = true;
8944
+ clearMobileFlowState();
8945
+ mobileSetupFlowRef.current = false;
8946
+ setupAccountIdRef.current = null;
8960
8947
  dispatch({
8961
- type: "RESTORE_GUEST_PREAUTH_MOBILE",
8962
- transfer,
8963
- guestSessionToken: persisted.guestSessionToken,
8964
- guestPreauthAccountId,
8965
- guestPreauthSessionId: persisted.sessionId,
8966
- selectedProviderId: persisted.providerId,
8967
- deeplinkUri: persisted.deeplinkUri,
8968
- mobileOpen
8948
+ type: "GUEST_PREAUTH_DETECTED",
8949
+ accountId: guestLookup.accountId,
8950
+ sessionId: persisted.sessionId ?? void 0
8969
8951
  });
8970
- if (mobileOpen) {
8971
- mobileSetupFlowRef.current = true;
8972
- setupAccountIdRef.current = persisted.accountId ?? null;
8973
- } else {
8974
- clearMobileFlowState();
8952
+ dispatch({ type: "REQUEST_LOGIN" });
8953
+ dispatch({ type: "MOBILE_SETUP_COMPLETE" });
8954
+ };
8955
+ const pollGuestAccount = async () => {
8956
+ try {
8957
+ if (cancelled) return;
8958
+ const guestLookup = await fetchGuestAccount(apiBaseUrl, guestToken);
8959
+ if (cancelled) return;
8960
+ if (guestLookup != null) {
8961
+ await handleAccountFound(guestLookup);
8962
+ }
8963
+ } catch {
8975
8964
  }
8976
- } catch (err) {
8977
- guestPreauthRestoreAttemptedKeys.delete(key);
8978
- captureException(err);
8979
- }
8980
- })();
8965
+ };
8966
+ pollGuestAccount();
8967
+ const intervalId = window.setInterval(pollGuestAccount, POLL_INTERVAL_MS);
8968
+ const handlePollVisibility = () => {
8969
+ if (document.visibilityState === "visible" && !cancelled) void pollGuestAccount();
8970
+ };
8971
+ const handlePollPageShow = (e) => {
8972
+ if (e.persisted && !cancelled) void pollGuestAccount();
8973
+ };
8974
+ document.addEventListener("visibilitychange", handlePollVisibility);
8975
+ window.addEventListener("pageshow", handlePollPageShow);
8976
+ pollingCleanupRef.current = () => {
8977
+ cancelled = true;
8978
+ window.clearInterval(intervalId);
8979
+ document.removeEventListener("visibilitychange", handlePollVisibility);
8980
+ window.removeEventListener("pageshow", handlePollPageShow);
8981
+ if (!completedRef.current) {
8982
+ guestPreauthRestoreAttemptedKeys.delete(key);
8983
+ }
8984
+ pollingCleanupRef.current = null;
8985
+ };
8986
+ };
8987
+ const onOuterVisibility = () => {
8988
+ if (document.visibilityState === "visible") tryStart();
8989
+ };
8990
+ const onOuterPageShow = (e) => {
8991
+ if (e.persisted) tryStart();
8992
+ };
8993
+ tryStart();
8994
+ document.addEventListener("visibilitychange", onOuterVisibility);
8995
+ window.addEventListener("pageshow", onOuterPageShow);
8981
8996
  return () => {
8982
- cancelled = true;
8997
+ document.removeEventListener("visibilitychange", onOuterVisibility);
8998
+ window.removeEventListener("pageshow", onOuterPageShow);
8999
+ pollingCleanupRef.current?.();
8983
9000
  };
8984
- }, [privyReady, authenticated, apiBaseUrl, dispatch, mobileSetupFlowRef, setupAccountIdRef]);
9001
+ }, [privyReady, apiBaseUrl, dispatch, mobileSetupFlowRef, setupAccountIdRef]);
8985
9002
  }
8986
9003
  function useSelectSourceEffect(deps) {
8987
9004
  const {
@@ -9424,7 +9441,6 @@ function BlinkPaymentInner({
9424
9441
  dispatch,
9425
9442
  apiBaseUrl,
9426
9443
  privyReady: state.privyReady,
9427
- authenticated,
9428
9444
  mobileSetupFlowRef: mobileFlowRefs.mobileSetupFlowRef,
9429
9445
  setupAccountIdRef: mobileFlowRefs.setupAccountIdRef
9430
9446
  });