@worldcoin/idkit 4.0.7 → 4.0.9-dev.e3dec21

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.js CHANGED
@@ -1,6 +1,6 @@
1
- import { IDKitErrorCodes, IDKit } from '@worldcoin/idkit-core';
2
- export { IDKit, IDKitErrorCodes, documentLegacy, orbLegacy, secureDocumentLegacy, selfieCheckLegacy, signRequest } from '@worldcoin/idkit-core';
3
- import { memo, useMemo, useState, useRef, useEffect, useCallback } from 'react';
1
+ import { IDKitErrorCodes, isInWorldApp, IDKit } from '@worldcoin/idkit-core';
2
+ export { IDKit, IDKitErrorCodes, deviceLegacy, documentLegacy, orbLegacy, secureDocumentLegacy, selfieCheckLegacy, signRequest } from '@worldcoin/idkit-core';
3
+ import { memo, useMemo, useState, useRef, useCallback, useEffect } from 'react';
4
4
  import { createPortal } from 'react-dom';
5
5
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
6
6
  import QRCodeUtil from 'qrcode/lib/core/qrcode.js';
@@ -60,10 +60,13 @@ function toErrorCode(error) {
60
60
  }
61
61
 
62
62
  // src/hooks/useIDKitFlow.ts
63
+ var isDebug = () => typeof window !== "undefined" && window.IDKIT_DEBUG;
63
64
  function useIDKitFlow(createFlowHandle, config) {
65
+ const isInWorldApp$1 = useMemo(() => isInWorldApp(), []);
64
66
  const [state, setState] = useState(
65
67
  createInitialHookState
66
68
  );
69
+ const [runId, setRunId] = useState(0);
67
70
  const abortRef = useRef(null);
68
71
  const createFlowHandleRef = useRef(createFlowHandle);
69
72
  const configRef = useRef(config);
@@ -73,6 +76,7 @@ function useIDKitFlow(createFlowHandle, config) {
73
76
  abortRef.current?.abort();
74
77
  abortRef.current = null;
75
78
  setState(createInitialHookState);
79
+ setRunId((id) => id + 1);
76
80
  }, []);
77
81
  const open = useCallback(() => {
78
82
  setState((prev) => {
@@ -108,13 +112,20 @@ function useIDKitFlow(createFlowHandle, config) {
108
112
  };
109
113
  void (async () => {
110
114
  try {
115
+ if (isDebug()) console.debug("[IDKit] Creating flow handle\u2026");
111
116
  const request = await createFlowHandleRef.current();
112
117
  ensureNotAborted(controller.signal);
118
+ if (isDebug())
119
+ console.debug("[IDKit] Flow created", {
120
+ connectorURI: request.connectorURI,
121
+ requestId: request.requestId
122
+ });
123
+ const connectorURI = isInWorldApp$1 ? null : request.connectorURI;
113
124
  setState((prev) => {
114
- if (prev.connectorURI === request.connectorURI) {
125
+ if (prev.connectorURI === connectorURI) {
115
126
  return prev;
116
127
  }
117
- return { ...prev, connectorURI: request.connectorURI };
128
+ return { ...prev, connectorURI };
118
129
  });
119
130
  const pollInterval = configRef.current.polling?.interval ?? 1e3;
120
131
  const timeout = configRef.current.polling?.timeout ?? 3e5;
@@ -142,6 +153,8 @@ function useIDKitFlow(createFlowHandle, config) {
142
153
  return;
143
154
  }
144
155
  if (nextStatus.type === "failed") {
156
+ if (isDebug())
157
+ console.warn("[IDKit] Poll returned failed", nextStatus);
145
158
  setFailed(nextStatus.error ?? IDKitErrorCodes.GenericError);
146
159
  return;
147
160
  }
@@ -155,8 +168,10 @@ function useIDKitFlow(createFlowHandle, config) {
155
168
  }
156
169
  } catch (error) {
157
170
  if (controller.signal.aborted) {
171
+ if (isDebug()) console.debug("[IDKit] Flow aborted");
158
172
  return;
159
173
  }
174
+ if (isDebug()) console.error("[IDKit] Flow error:", error);
160
175
  setFailed(toErrorCode(error));
161
176
  }
162
177
  })();
@@ -166,7 +181,7 @@ function useIDKitFlow(createFlowHandle, config) {
166
181
  abortRef.current = null;
167
182
  }
168
183
  };
169
- }, [state.isOpen]);
184
+ }, [state.isOpen, runId, isInWorldApp$1]);
170
185
  return {
171
186
  open,
172
187
  reset,
@@ -177,7 +192,8 @@ function useIDKitFlow(createFlowHandle, config) {
177
192
  connectorURI: state.connectorURI,
178
193
  result: state.result,
179
194
  errorCode: state.errorCode,
180
- isOpen: state.isOpen
195
+ isOpen: state.isOpen,
196
+ isInWorldApp: isInWorldApp$1
181
197
  };
182
198
  }
183
199
 
@@ -1622,7 +1638,8 @@ function getVisualStage(isSuccess, isError, isHostVerifying) {
1622
1638
  if (isSuccess) return "success";
1623
1639
  return "worldid";
1624
1640
  }
1625
- function IDKitRequestWidget({
1641
+ function IDKitWidgetBase({
1642
+ flow,
1626
1643
  open,
1627
1644
  onOpenChange,
1628
1645
  handleVerify,
@@ -1630,12 +1647,8 @@ function IDKitRequestWidget({
1630
1647
  onError,
1631
1648
  autoClose = true,
1632
1649
  language,
1633
- ...config
1650
+ showSimulatorCallout
1634
1651
  }) {
1635
- if (typeof onSuccess !== "function") {
1636
- throw new Error("IDKitRequestWidget requires an onSuccess callback.");
1637
- }
1638
- const flow = useIDKitRequest(config);
1639
1652
  const { open: openFlow, reset: resetFlow } = flow;
1640
1653
  const [hostVerifyResult, setHostVerifyResult] = useState(null);
1641
1654
  const lastResultRef = useRef(null);
@@ -1677,13 +1690,31 @@ function IDKitRequestWidget({
1677
1690
  });
1678
1691
  }, [effectiveErrorCode, onError]);
1679
1692
  useEffect(() => {
1680
- if (isSuccess && autoClose) {
1693
+ if (!flow.isInWorldApp || !isHostVerifying || !flow.result || !handleVerify) {
1694
+ return;
1695
+ }
1696
+ let cancelled = false;
1697
+ void Promise.resolve(handleVerify(flow.result)).then(() => {
1698
+ if (!cancelled) setHostVerifyResult("passed");
1699
+ }).catch(() => {
1700
+ if (!cancelled) setHostVerifyResult("failed");
1701
+ });
1702
+ return () => {
1703
+ cancelled = true;
1704
+ };
1705
+ }, [flow.isInWorldApp, isHostVerifying, flow.result, handleVerify]);
1706
+ useEffect(() => {
1707
+ if (flow.isInWorldApp && (isSuccess || isError)) {
1708
+ onOpenChange(false);
1709
+ } else if (isSuccess && autoClose) {
1681
1710
  const timer = setTimeout(() => onOpenChange(false), 2500);
1682
1711
  return () => clearTimeout(timer);
1683
1712
  }
1684
- }, [isSuccess, autoClose, onOpenChange]);
1713
+ }, [isSuccess, isError, autoClose, onOpenChange, flow.isInWorldApp]);
1714
+ if (flow.isInWorldApp) {
1715
+ return null;
1716
+ }
1685
1717
  const stage = getVisualStage(isSuccess, isError, isHostVerifying);
1686
- const showSimulatorCallout = config.environment === "staging";
1687
1718
  return /* @__PURE__ */ jsxs(IDKitModal, { open, onOpenChange, children: [
1688
1719
  stage === "worldid" && /* @__PURE__ */ jsx(
1689
1720
  WorldIDState,
@@ -1717,6 +1748,35 @@ function IDKitRequestWidget({
1717
1748
  )
1718
1749
  ] });
1719
1750
  }
1751
+ function IDKitRequestWidget({
1752
+ open,
1753
+ onOpenChange,
1754
+ handleVerify,
1755
+ onSuccess,
1756
+ onError,
1757
+ autoClose,
1758
+ language,
1759
+ ...config
1760
+ }) {
1761
+ if (typeof onSuccess !== "function") {
1762
+ throw new Error("IDKitRequestWidget requires an onSuccess callback.");
1763
+ }
1764
+ const flow = useIDKitRequest(config);
1765
+ return /* @__PURE__ */ jsx(
1766
+ IDKitWidgetBase,
1767
+ {
1768
+ flow,
1769
+ open,
1770
+ onOpenChange,
1771
+ handleVerify,
1772
+ onSuccess,
1773
+ onError,
1774
+ autoClose,
1775
+ language,
1776
+ showSimulatorCallout: config.environment === "staging"
1777
+ }
1778
+ );
1779
+ }
1720
1780
 
1721
1781
  export { IDKitRequestWidget, useIDKitRequest };
1722
1782
  //# sourceMappingURL=index.js.map