@calimero-network/mero-react 10.2.0 → 10.3.0

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/README.md CHANGED
@@ -92,6 +92,7 @@ Props (`MeroProviderConfig & { children }`):
92
92
  | `registryUrl` | `string` | No | Registry URL override |
93
93
  | `timeoutMs` | `number` | No | HTTP request timeout (default 30000) |
94
94
  | `allowedNodeUrls` | `string[]` | No | Origins the OAuth callback may authenticate against. The initiated node is always trusted; this allowlist additionally permits direct-callback entry. A callback `node_url` matching neither is rejected. |
95
+ | `servedByNode` | `boolean` | No | The node serves this page from its own origin. See [Serving an app from a node](#serving-an-app-from-a-node). |
95
96
  | `tokenStore` | `TokenStore` | No | Pluggable access/refresh token store. Defaults to localStorage. Pass a `MemoryTokenStore` or cookie-backed store for sensitive deployments (localStorage tokens are XSS-readable). |
96
97
 
97
98
  Modes and their permissions:
@@ -104,6 +105,22 @@ Modes and their permissions:
104
105
 
105
106
  Auth flow: when `connectToNode(url)` is called, the provider redirects to the node's auth page. After login, the node redirects back with tokens in the URL hash. The provider processes these once (StrictMode-safe via ref) and sets `isAuthenticated = true`. The callback's `node_url` is validated against the node login was initiated with (or `allowedNodeUrls`); a `node_url` matching neither is rejected.
106
107
 
108
+ #### Serving an app from a node
109
+
110
+ When a node serves your bundled frontend (for example at `my-app.localhost:2428`), pass `servedByNode`.
111
+ The node is then the page's own origin: the client talks to `window.location.origin` whatever the login callback's `node_url` says, and nothing probes other origins.
112
+ `ConnectButton` skips the node picker and goes straight to `/auth/login` on that origin, which the node redirects to its login page.
113
+ Set the prop yourself, for example from a build flag; the provider does not detect it.
114
+
115
+ ```tsx
116
+ <MeroProvider mode={AppMode.MultiContext} servedByNode>
117
+ ```
118
+
119
+ The node serves the page under a strict CSP (`connect-src 'self'`), so the bundle must:
120
+
121
+ - be built with Vite `base: './'` and reference assets by relative URL;
122
+ - have no inline scripts and no CDN-hosted scripts, styles or fonts.
123
+
107
124
  Online detection: the provider opens an SSE connection to the node after auth. `isOnline` reflects the SSE connection state — no polling.
108
125
 
109
126
  ### `useMero()`
@@ -413,7 +430,7 @@ Helpers: `defaultMeroTheme` (the full default palette), `resolveMeroTheme(partia
413
430
 
414
431
  | Prop | Type | Default | Notes |
415
432
  |------|------|---------|-------|
416
- | `connectionType` | `ConnectionType \| CustomConnectionConfig` | `Remote` | `Custom` connects straight to the given URL, skipping the modal. Every other value opens the LoginModal (node discovery + manual URL entry). |
433
+ | `connectionType` | `ConnectionType \| CustomConnectionConfig` | `Remote` | `Custom` connects straight to the given URL, skipping the modal. Every other value opens the LoginModal (local node discovery + manual URL entry). Inside a `servedByNode` provider the button skips the modal and logs in at the page's own origin. |
417
434
  | `theme` | `MeroTheme` | — | Partial token override. |
418
435
  | `logoOnly` | `boolean` | `false` | Render only the Calimero logo (square button). The label is still announced via `aria-label`. |
419
436
  | `label` | `string \| { connect?, connected?, reconnecting? }` | — | Override default labels. Bare string targets the disconnected state. |
package/dist/index.cjs CHANGED
@@ -346,8 +346,10 @@ function MeroProvider({
346
346
  cloudBaseUrl,
347
347
  timeoutMs = 3e4,
348
348
  allowedNodeUrls,
349
+ servedByNode,
349
350
  tokenStore: tokenStoreProp
350
351
  }) {
352
+ const ownOrigin = servedByNode && isBrowser ? window.location.origin : null;
351
353
  const tokenStore = react.useMemo(
352
354
  () => tokenStoreProp ?? new meroJs.LocalStorageTokenStore(),
353
355
  [tokenStoreProp]
@@ -359,7 +361,7 @@ function MeroProvider({
359
361
  const [isAuthenticated, setIsAuthenticated] = react.useState(false);
360
362
  const [isOnline, setIsOnline] = react.useState(true);
361
363
  const [isLoading, setIsLoading] = react.useState(true);
362
- const [nodeUrl, setNodeUrlState] = react.useState(() => getNodeUrl());
364
+ const [nodeUrl, setNodeUrlState] = react.useState(() => ownOrigin ?? getNodeUrl());
363
365
  const [applicationId, setApplicationIdState] = react.useState(
364
366
  () => getApplicationId() || null
365
367
  );
@@ -377,14 +379,14 @@ function MeroProvider({
377
379
  meroRef.current.close();
378
380
  }
379
381
  const instance = new meroJs.MeroJs({
380
- baseUrl: url,
382
+ baseUrl: ownOrigin ?? url,
381
383
  tokenStore,
382
384
  timeoutMs
383
385
  });
384
386
  meroRef.current = instance;
385
387
  return instance;
386
388
  },
387
- [timeoutMs, tokenStore]
389
+ [timeoutMs, tokenStore, ownOrigin]
388
390
  );
389
391
  const checkAuth = react.useCallback(
390
392
  async (instance) => {
@@ -442,12 +444,12 @@ function MeroProvider({
442
444
  setDelegated(null);
443
445
  setMero(null);
444
446
  setIsAuthenticated(false);
445
- setNodeUrlState(null);
447
+ setNodeUrlState(ownOrigin);
446
448
  setApplicationIdState(null);
447
449
  setContextIdState(null);
448
450
  setContextIdentityState(null);
449
451
  meroRef.current = null;
450
- }, [tokenStore]);
452
+ }, [tokenStore, ownOrigin]);
451
453
  react.useEffect(() => {
452
454
  if (!delegated) return;
453
455
  const relayUrl = (contextId ? meroJs.relayForContext(delegated.account, contextId) : null) ?? delegated.relayUrl;
@@ -462,8 +464,9 @@ function MeroProvider({
462
464
  let active = true;
463
465
  const stop = new AbortController();
464
466
  (async () => {
465
- if (!client || !relayUrl) return;
466
- if (awaitingKey) {
467
+ const hasRelay = Boolean(client && relayUrl);
468
+ if (!hasRelay && !packageName) return;
469
+ if (awaitingKey && relayUrl) {
467
470
  const nodeKey = await meroJs.learnRelayNodeKey(relayUrl, { signal: stop.signal });
468
471
  if (!active) return;
469
472
  setMero(nodeKey ? meroJs.buildDelegatedClient(routed, contextId) ?? client : client);
@@ -484,6 +487,7 @@ function MeroProvider({
484
487
  if (!active) return;
485
488
  }
486
489
  }
490
+ if (!hasRelay) return;
487
491
  try {
488
492
  const contexts = await meroJs.listDelegatedContexts(delegated);
489
493
  if (!active) return;
@@ -512,7 +516,7 @@ function MeroProvider({
512
516
  if (callback) {
513
517
  const initiatedNodeUrl = getNodeUrl();
514
518
  const tokenNodeUrl = getTokenNodeUrl() ?? initiatedNodeUrl;
515
- const { url, rejected } = resolveTrustedNodeUrl({
519
+ const { url, rejected } = ownOrigin ? { url: ownOrigin, rejected: false } : resolveTrustedNodeUrl({
516
520
  candidate: callback.nodeUrl,
517
521
  initiated: initiatedNodeUrl,
518
522
  allowedNodeUrls
@@ -556,7 +560,7 @@ function MeroProvider({
556
560
  nodeFromCallback = url;
557
561
  }
558
562
  }
559
- const savedUrl = nodeFromCallback || getNodeUrl();
563
+ const savedUrl = nodeFromCallback || ownOrigin || getNodeUrl();
560
564
  if (!savedUrl) {
561
565
  if (active) setIsLoading(false);
562
566
  return;
@@ -580,7 +584,7 @@ function MeroProvider({
580
584
  return () => {
581
585
  active = false;
582
586
  };
583
- }, [createMeroInstance, checkAuth, allowedNodeUrls, delegated]);
587
+ }, [createMeroInstance, checkAuth, allowedNodeUrls, delegated, ownOrigin]);
584
588
  react.useEffect(() => {
585
589
  if (!isAuthenticated || !meroRef.current) return;
586
590
  let active = true;
@@ -671,6 +675,7 @@ function MeroProvider({
671
675
  connectToNode,
672
676
  connectWithAccount,
673
677
  isDelegated: delegated !== null,
678
+ servedByNode: ownOrigin !== null,
674
679
  admin,
675
680
  can: delegated !== null ? { createNamespace: Boolean(packageName), createContext: true, invite: true, upgrade: false } : { createNamespace: true, createContext: true, invite: true, upgrade: true },
676
681
  app: { packageName, packageVersion, registryUrl },
@@ -678,7 +683,7 @@ function MeroProvider({
678
683
  logout,
679
684
  isLoading
680
685
  }),
681
- [mero, admin, isAuthenticated, isOnline, nodeUrl, applicationId, contextId, contextIdentity, connectToNode, connectWithAccount, delegated, logout, isLoading, packageName, packageVersion, registryUrl, cloudBaseUrl]
686
+ [mero, admin, isAuthenticated, isOnline, nodeUrl, applicationId, contextId, contextIdentity, connectToNode, connectWithAccount, delegated, ownOrigin, logout, isLoading, packageName, packageVersion, registryUrl, cloudBaseUrl]
682
687
  );
683
688
  return /* @__PURE__ */ jsxRuntime.jsx(MeroContext.Provider, { value: contextValue, children });
684
689
  }
@@ -1330,7 +1335,8 @@ function LoginModal({
1330
1335
  cloudOnDesktop = false,
1331
1336
  initialTab: initialTabProp
1332
1337
  }) {
1333
- const cloudAllowed = cloudOnDesktop || !isDesktopWindow();
1338
+ const servedByNode = react.useContext(MeroContext)?.servedByNode ?? false;
1339
+ const cloudAllowed = !servedByNode && (cloudOnDesktop || !isDesktopWindow());
1334
1340
  const ownCloud = cloud === void 0 && cloudAllowed;
1335
1341
  const own = useAccountEnrolment({ enabled: ownCloud });
1336
1342
  const cloudTab = react.useMemo(
@@ -1372,10 +1378,13 @@ function LoginModal({
1372
1378
  setCustomUrl(savedUrl);
1373
1379
  }
1374
1380
  }, []);
1381
+ react.useEffect(() => {
1382
+ if (isOpen && servedByNode) onConnect(window.location.origin);
1383
+ }, [isOpen, servedByNode]);
1375
1384
  const [scanNonce, setScanNonce] = react.useState(0);
1376
1385
  const portsKey = react.useMemo(() => localNodePorts.join(","), [localNodePorts]);
1377
1386
  react.useEffect(() => {
1378
- if (!isOpen) {
1387
+ if (!isOpen || servedByNode) {
1379
1388
  return;
1380
1389
  }
1381
1390
  const controller2 = new AbortController();
@@ -1396,7 +1405,7 @@ function LoginModal({
1396
1405
  active = false;
1397
1406
  controller2.abort();
1398
1407
  };
1399
- }, [isOpen, portsKey, scanNonce]);
1408
+ }, [isOpen, servedByNode, portsKey, scanNonce]);
1400
1409
  const isCustom = selected === CUSTOM_SELECTION;
1401
1410
  const hasDiscovered = discovered.length > 0;
1402
1411
  const canConnect = !loading && !discovering && (isCustom ? isValidUrl(customUrl) : true);
@@ -1431,7 +1440,7 @@ function LoginModal({
1431
1440
  setLoading(false);
1432
1441
  }
1433
1442
  }, [selected, customUrl, onConnect]);
1434
- if (!isOpen) {
1443
+ if (!isOpen || servedByNode) {
1435
1444
  return null;
1436
1445
  }
1437
1446
  const showManualInput = isCustom;
@@ -1709,8 +1718,8 @@ function ConnectButton({
1709
1718
  cloud: cloudProp,
1710
1719
  accountDefaults
1711
1720
  }) {
1712
- const cloud = cloudProp ?? !isDesktopWindow();
1713
- const { isAuthenticated, connectToNode, logout, nodeUrl, isOnline, isDelegated } = useMero();
1721
+ const { isAuthenticated, connectToNode, logout, nodeUrl, isOnline, isDelegated, servedByNode } = useMero();
1722
+ const cloud = !servedByNode && (cloudProp ?? !isDesktopWindow());
1714
1723
  const enrolment = useAccountEnrolment({
1715
1724
  walletUrl: accountDefaults?.walletUrl,
1716
1725
  enabled: cloud
@@ -1753,6 +1762,10 @@ function ConnectButton({
1753
1762
  }, [label]);
1754
1763
  const buttonClassName = logoOnly ? "mero-logo-only" : "";
1755
1764
  const handleConnect = () => {
1765
+ if (servedByNode) {
1766
+ connectToNode(window.location.origin);
1767
+ return;
1768
+ }
1756
1769
  if (typeof connectionType === "object" && connectionType.type === "custom" /* Custom */) {
1757
1770
  connectToNode(connectionType.url);
1758
1771
  return;
@@ -1891,8 +1904,8 @@ function ConnectButtonAccount({
1891
1904
  defaults = {},
1892
1905
  cloud: cloudProp
1893
1906
  }) {
1894
- const cloud = cloudProp ?? !isDesktopWindow();
1895
- const { isAuthenticated, isOnline, nodeUrl, isDelegated, connectToNode, logout } = useMero();
1907
+ const { isAuthenticated, isOnline, nodeUrl, isDelegated, connectToNode, logout, servedByNode } = useMero();
1908
+ const cloud = !servedByNode && (cloudProp ?? !isDesktopWindow());
1896
1909
  const [isDropdownOpen, setIsDropdownOpen] = react.useState(false);
1897
1910
  const [isNodeModalOpen, setIsNodeModalOpen] = react.useState(false);
1898
1911
  const [isAccountOpen, setIsAccountOpen] = react.useState(false);
@@ -1986,7 +1999,7 @@ function ConnectButtonAccount({
1986
1999
  {
1987
2000
  className: ["mero-connect-button", className].filter(Boolean).join(" "),
1988
2001
  style,
1989
- onClick: () => setIsNodeModalOpen(true),
2002
+ onClick: () => servedByNode ? connectToNode(window.location.origin) : setIsNodeModalOpen(true),
1990
2003
  children: [
1991
2004
  /* @__PURE__ */ jsxRuntime.jsx(CalimeroLogo, { size: 18, className: "mero-logo" }),
1992
2005
  "I run a node"