@calimero-network/mero-react 10.2.1 → 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;
@@ -514,7 +516,7 @@ function MeroProvider({
514
516
  if (callback) {
515
517
  const initiatedNodeUrl = getNodeUrl();
516
518
  const tokenNodeUrl = getTokenNodeUrl() ?? initiatedNodeUrl;
517
- const { url, rejected } = resolveTrustedNodeUrl({
519
+ const { url, rejected } = ownOrigin ? { url: ownOrigin, rejected: false } : resolveTrustedNodeUrl({
518
520
  candidate: callback.nodeUrl,
519
521
  initiated: initiatedNodeUrl,
520
522
  allowedNodeUrls
@@ -558,7 +560,7 @@ function MeroProvider({
558
560
  nodeFromCallback = url;
559
561
  }
560
562
  }
561
- const savedUrl = nodeFromCallback || getNodeUrl();
563
+ const savedUrl = nodeFromCallback || ownOrigin || getNodeUrl();
562
564
  if (!savedUrl) {
563
565
  if (active) setIsLoading(false);
564
566
  return;
@@ -582,7 +584,7 @@ function MeroProvider({
582
584
  return () => {
583
585
  active = false;
584
586
  };
585
- }, [createMeroInstance, checkAuth, allowedNodeUrls, delegated]);
587
+ }, [createMeroInstance, checkAuth, allowedNodeUrls, delegated, ownOrigin]);
586
588
  react.useEffect(() => {
587
589
  if (!isAuthenticated || !meroRef.current) return;
588
590
  let active = true;
@@ -673,6 +675,7 @@ function MeroProvider({
673
675
  connectToNode,
674
676
  connectWithAccount,
675
677
  isDelegated: delegated !== null,
678
+ servedByNode: ownOrigin !== null,
676
679
  admin,
677
680
  can: delegated !== null ? { createNamespace: Boolean(packageName), createContext: true, invite: true, upgrade: false } : { createNamespace: true, createContext: true, invite: true, upgrade: true },
678
681
  app: { packageName, packageVersion, registryUrl },
@@ -680,7 +683,7 @@ function MeroProvider({
680
683
  logout,
681
684
  isLoading
682
685
  }),
683
- [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]
684
687
  );
685
688
  return /* @__PURE__ */ jsxRuntime.jsx(MeroContext.Provider, { value: contextValue, children });
686
689
  }
@@ -1332,7 +1335,8 @@ function LoginModal({
1332
1335
  cloudOnDesktop = false,
1333
1336
  initialTab: initialTabProp
1334
1337
  }) {
1335
- const cloudAllowed = cloudOnDesktop || !isDesktopWindow();
1338
+ const servedByNode = react.useContext(MeroContext)?.servedByNode ?? false;
1339
+ const cloudAllowed = !servedByNode && (cloudOnDesktop || !isDesktopWindow());
1336
1340
  const ownCloud = cloud === void 0 && cloudAllowed;
1337
1341
  const own = useAccountEnrolment({ enabled: ownCloud });
1338
1342
  const cloudTab = react.useMemo(
@@ -1374,10 +1378,13 @@ function LoginModal({
1374
1378
  setCustomUrl(savedUrl);
1375
1379
  }
1376
1380
  }, []);
1381
+ react.useEffect(() => {
1382
+ if (isOpen && servedByNode) onConnect(window.location.origin);
1383
+ }, [isOpen, servedByNode]);
1377
1384
  const [scanNonce, setScanNonce] = react.useState(0);
1378
1385
  const portsKey = react.useMemo(() => localNodePorts.join(","), [localNodePorts]);
1379
1386
  react.useEffect(() => {
1380
- if (!isOpen) {
1387
+ if (!isOpen || servedByNode) {
1381
1388
  return;
1382
1389
  }
1383
1390
  const controller2 = new AbortController();
@@ -1398,7 +1405,7 @@ function LoginModal({
1398
1405
  active = false;
1399
1406
  controller2.abort();
1400
1407
  };
1401
- }, [isOpen, portsKey, scanNonce]);
1408
+ }, [isOpen, servedByNode, portsKey, scanNonce]);
1402
1409
  const isCustom = selected === CUSTOM_SELECTION;
1403
1410
  const hasDiscovered = discovered.length > 0;
1404
1411
  const canConnect = !loading && !discovering && (isCustom ? isValidUrl(customUrl) : true);
@@ -1433,7 +1440,7 @@ function LoginModal({
1433
1440
  setLoading(false);
1434
1441
  }
1435
1442
  }, [selected, customUrl, onConnect]);
1436
- if (!isOpen) {
1443
+ if (!isOpen || servedByNode) {
1437
1444
  return null;
1438
1445
  }
1439
1446
  const showManualInput = isCustom;
@@ -1711,8 +1718,8 @@ function ConnectButton({
1711
1718
  cloud: cloudProp,
1712
1719
  accountDefaults
1713
1720
  }) {
1714
- const cloud = cloudProp ?? !isDesktopWindow();
1715
- const { isAuthenticated, connectToNode, logout, nodeUrl, isOnline, isDelegated } = useMero();
1721
+ const { isAuthenticated, connectToNode, logout, nodeUrl, isOnline, isDelegated, servedByNode } = useMero();
1722
+ const cloud = !servedByNode && (cloudProp ?? !isDesktopWindow());
1716
1723
  const enrolment = useAccountEnrolment({
1717
1724
  walletUrl: accountDefaults?.walletUrl,
1718
1725
  enabled: cloud
@@ -1755,6 +1762,10 @@ function ConnectButton({
1755
1762
  }, [label]);
1756
1763
  const buttonClassName = logoOnly ? "mero-logo-only" : "";
1757
1764
  const handleConnect = () => {
1765
+ if (servedByNode) {
1766
+ connectToNode(window.location.origin);
1767
+ return;
1768
+ }
1758
1769
  if (typeof connectionType === "object" && connectionType.type === "custom" /* Custom */) {
1759
1770
  connectToNode(connectionType.url);
1760
1771
  return;
@@ -1893,8 +1904,8 @@ function ConnectButtonAccount({
1893
1904
  defaults = {},
1894
1905
  cloud: cloudProp
1895
1906
  }) {
1896
- const cloud = cloudProp ?? !isDesktopWindow();
1897
- const { isAuthenticated, isOnline, nodeUrl, isDelegated, connectToNode, logout } = useMero();
1907
+ const { isAuthenticated, isOnline, nodeUrl, isDelegated, connectToNode, logout, servedByNode } = useMero();
1908
+ const cloud = !servedByNode && (cloudProp ?? !isDesktopWindow());
1898
1909
  const [isDropdownOpen, setIsDropdownOpen] = react.useState(false);
1899
1910
  const [isNodeModalOpen, setIsNodeModalOpen] = react.useState(false);
1900
1911
  const [isAccountOpen, setIsAccountOpen] = react.useState(false);
@@ -1988,7 +1999,7 @@ function ConnectButtonAccount({
1988
1999
  {
1989
2000
  className: ["mero-connect-button", className].filter(Boolean).join(" "),
1990
2001
  style,
1991
- onClick: () => setIsNodeModalOpen(true),
2002
+ onClick: () => servedByNode ? connectToNode(window.location.origin) : setIsNodeModalOpen(true),
1992
2003
  children: [
1993
2004
  /* @__PURE__ */ jsxRuntime.jsx(CalimeroLogo, { size: 18, className: "mero-logo" }),
1994
2005
  "I run a node"