@worldcoin/idkit 4.0.8 → 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.cjs CHANGED
@@ -65,10 +65,13 @@ function toErrorCode(error) {
65
65
  }
66
66
 
67
67
  // src/hooks/useIDKitFlow.ts
68
+ var isDebug = () => typeof window !== "undefined" && window.IDKIT_DEBUG;
68
69
  function useIDKitFlow(createFlowHandle, config) {
70
+ const isInWorldApp = react.useMemo(() => idkitCore.isInWorldApp(), []);
69
71
  const [state, setState] = react.useState(
70
72
  createInitialHookState
71
73
  );
74
+ const [runId, setRunId] = react.useState(0);
72
75
  const abortRef = react.useRef(null);
73
76
  const createFlowHandleRef = react.useRef(createFlowHandle);
74
77
  const configRef = react.useRef(config);
@@ -78,6 +81,7 @@ function useIDKitFlow(createFlowHandle, config) {
78
81
  abortRef.current?.abort();
79
82
  abortRef.current = null;
80
83
  setState(createInitialHookState);
84
+ setRunId((id) => id + 1);
81
85
  }, []);
82
86
  const open = react.useCallback(() => {
83
87
  setState((prev) => {
@@ -113,13 +117,20 @@ function useIDKitFlow(createFlowHandle, config) {
113
117
  };
114
118
  void (async () => {
115
119
  try {
120
+ if (isDebug()) console.debug("[IDKit] Creating flow handle\u2026");
116
121
  const request = await createFlowHandleRef.current();
117
122
  ensureNotAborted(controller.signal);
123
+ if (isDebug())
124
+ console.debug("[IDKit] Flow created", {
125
+ connectorURI: request.connectorURI,
126
+ requestId: request.requestId
127
+ });
128
+ const connectorURI = isInWorldApp ? null : request.connectorURI;
118
129
  setState((prev) => {
119
- if (prev.connectorURI === request.connectorURI) {
130
+ if (prev.connectorURI === connectorURI) {
120
131
  return prev;
121
132
  }
122
- return { ...prev, connectorURI: request.connectorURI };
133
+ return { ...prev, connectorURI };
123
134
  });
124
135
  const pollInterval = configRef.current.polling?.interval ?? 1e3;
125
136
  const timeout = configRef.current.polling?.timeout ?? 3e5;
@@ -147,6 +158,8 @@ function useIDKitFlow(createFlowHandle, config) {
147
158
  return;
148
159
  }
149
160
  if (nextStatus.type === "failed") {
161
+ if (isDebug())
162
+ console.warn("[IDKit] Poll returned failed", nextStatus);
150
163
  setFailed(nextStatus.error ?? idkitCore.IDKitErrorCodes.GenericError);
151
164
  return;
152
165
  }
@@ -160,8 +173,10 @@ function useIDKitFlow(createFlowHandle, config) {
160
173
  }
161
174
  } catch (error) {
162
175
  if (controller.signal.aborted) {
176
+ if (isDebug()) console.debug("[IDKit] Flow aborted");
163
177
  return;
164
178
  }
179
+ if (isDebug()) console.error("[IDKit] Flow error:", error);
165
180
  setFailed(toErrorCode(error));
166
181
  }
167
182
  })();
@@ -171,7 +186,7 @@ function useIDKitFlow(createFlowHandle, config) {
171
186
  abortRef.current = null;
172
187
  }
173
188
  };
174
- }, [state.isOpen]);
189
+ }, [state.isOpen, runId, isInWorldApp]);
175
190
  return {
176
191
  open,
177
192
  reset,
@@ -182,7 +197,8 @@ function useIDKitFlow(createFlowHandle, config) {
182
197
  connectorURI: state.connectorURI,
183
198
  result: state.result,
184
199
  errorCode: state.errorCode,
185
- isOpen: state.isOpen
200
+ isOpen: state.isOpen,
201
+ isInWorldApp
186
202
  };
187
203
  }
188
204
 
@@ -1627,7 +1643,8 @@ function getVisualStage(isSuccess, isError, isHostVerifying) {
1627
1643
  if (isSuccess) return "success";
1628
1644
  return "worldid";
1629
1645
  }
1630
- function IDKitRequestWidget({
1646
+ function IDKitWidgetBase({
1647
+ flow,
1631
1648
  open,
1632
1649
  onOpenChange,
1633
1650
  handleVerify,
@@ -1635,12 +1652,8 @@ function IDKitRequestWidget({
1635
1652
  onError,
1636
1653
  autoClose = true,
1637
1654
  language,
1638
- ...config
1655
+ showSimulatorCallout
1639
1656
  }) {
1640
- if (typeof onSuccess !== "function") {
1641
- throw new Error("IDKitRequestWidget requires an onSuccess callback.");
1642
- }
1643
- const flow = useIDKitRequest(config);
1644
1657
  const { open: openFlow, reset: resetFlow } = flow;
1645
1658
  const [hostVerifyResult, setHostVerifyResult] = react.useState(null);
1646
1659
  const lastResultRef = react.useRef(null);
@@ -1682,13 +1695,31 @@ function IDKitRequestWidget({
1682
1695
  });
1683
1696
  }, [effectiveErrorCode, onError]);
1684
1697
  react.useEffect(() => {
1685
- if (isSuccess && autoClose) {
1698
+ if (!flow.isInWorldApp || !isHostVerifying || !flow.result || !handleVerify) {
1699
+ return;
1700
+ }
1701
+ let cancelled = false;
1702
+ void Promise.resolve(handleVerify(flow.result)).then(() => {
1703
+ if (!cancelled) setHostVerifyResult("passed");
1704
+ }).catch(() => {
1705
+ if (!cancelled) setHostVerifyResult("failed");
1706
+ });
1707
+ return () => {
1708
+ cancelled = true;
1709
+ };
1710
+ }, [flow.isInWorldApp, isHostVerifying, flow.result, handleVerify]);
1711
+ react.useEffect(() => {
1712
+ if (flow.isInWorldApp && (isSuccess || isError)) {
1713
+ onOpenChange(false);
1714
+ } else if (isSuccess && autoClose) {
1686
1715
  const timer = setTimeout(() => onOpenChange(false), 2500);
1687
1716
  return () => clearTimeout(timer);
1688
1717
  }
1689
- }, [isSuccess, autoClose, onOpenChange]);
1718
+ }, [isSuccess, isError, autoClose, onOpenChange, flow.isInWorldApp]);
1719
+ if (flow.isInWorldApp) {
1720
+ return null;
1721
+ }
1690
1722
  const stage = getVisualStage(isSuccess, isError, isHostVerifying);
1691
- const showSimulatorCallout = config.environment === "staging";
1692
1723
  return /* @__PURE__ */ jsxRuntime.jsxs(IDKitModal, { open, onOpenChange, children: [
1693
1724
  stage === "worldid" && /* @__PURE__ */ jsxRuntime.jsx(
1694
1725
  WorldIDState,
@@ -1722,6 +1753,35 @@ function IDKitRequestWidget({
1722
1753
  )
1723
1754
  ] });
1724
1755
  }
1756
+ function IDKitRequestWidget({
1757
+ open,
1758
+ onOpenChange,
1759
+ handleVerify,
1760
+ onSuccess,
1761
+ onError,
1762
+ autoClose,
1763
+ language,
1764
+ ...config
1765
+ }) {
1766
+ if (typeof onSuccess !== "function") {
1767
+ throw new Error("IDKitRequestWidget requires an onSuccess callback.");
1768
+ }
1769
+ const flow = useIDKitRequest(config);
1770
+ return /* @__PURE__ */ jsxRuntime.jsx(
1771
+ IDKitWidgetBase,
1772
+ {
1773
+ flow,
1774
+ open,
1775
+ onOpenChange,
1776
+ handleVerify,
1777
+ onSuccess,
1778
+ onError,
1779
+ autoClose,
1780
+ language,
1781
+ showSimulatorCallout: config.environment === "staging"
1782
+ }
1783
+ );
1784
+ }
1725
1785
 
1726
1786
  Object.defineProperty(exports, "IDKit", {
1727
1787
  enumerable: true,