@swype-org/react-sdk 0.1.80 → 0.1.81

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
@@ -701,13 +701,17 @@ var POPUP_RESULT_TIMEOUT_MS = 12e4;
701
701
  var POPUP_CLOSED_POLL_MS = 500;
702
702
  function createPasskeyViaPopup(options) {
703
703
  return new Promise((resolve, reject) => {
704
- const encoded = btoa(JSON.stringify(options));
704
+ const channelId = `swype-pk-${Date.now()}-${Math.random().toString(36).slice(2)}`;
705
+ const payload = { ...options, channelId };
706
+ const encoded = btoa(JSON.stringify(payload));
705
707
  const popupUrl = `${window.location.origin}/passkey-register#${encoded}`;
706
708
  const popup = window.open(popupUrl, "swype-passkey");
707
709
  if (!popup) {
708
710
  reject(new Error("Pop-up blocked. Please allow pop-ups for this site and try again."));
709
711
  return;
710
712
  }
713
+ let settled = false;
714
+ const channel = typeof BroadcastChannel !== "undefined" ? new BroadcastChannel(channelId) : null;
711
715
  const timer = setTimeout(() => {
712
716
  cleanup();
713
717
  reject(new Error("Passkey creation timed out. Please try again."));
@@ -718,11 +722,11 @@ function createPasskeyViaPopup(options) {
718
722
  reject(new Error("Passkey setup window was closed before completing."));
719
723
  }
720
724
  }, POPUP_CLOSED_POLL_MS);
721
- const handler = (event) => {
722
- if (event.source !== popup) return;
723
- const data = event.data;
725
+ function handleResult(data) {
726
+ if (settled) return;
724
727
  if (!data || typeof data !== "object") return;
725
728
  if (data.type !== "swype:passkey-popup-result") return;
729
+ settled = true;
726
730
  cleanup();
727
731
  if (data.error) {
728
732
  reject(new Error(data.error));
@@ -731,13 +735,21 @@ function createPasskeyViaPopup(options) {
731
735
  } else {
732
736
  reject(new Error("Invalid passkey popup response."));
733
737
  }
738
+ }
739
+ if (channel) {
740
+ channel.onmessage = (event) => handleResult(event.data);
741
+ }
742
+ const postMessageHandler = (event) => {
743
+ if (event.source !== popup) return;
744
+ handleResult(event.data);
734
745
  };
746
+ window.addEventListener("message", postMessageHandler);
735
747
  function cleanup() {
736
748
  clearTimeout(timer);
737
749
  clearInterval(closedPoll);
738
- window.removeEventListener("message", handler);
750
+ window.removeEventListener("message", postMessageHandler);
751
+ channel?.close();
739
752
  }
740
- window.addEventListener("message", handler);
741
753
  });
742
754
  }
743
755