@orangecheck/auth-client 2.33.0 → 2.33.2

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.mjs CHANGED
@@ -4,6 +4,90 @@ import { startRegistration, startAuthentication } from '@simplewebauthn/browser'
4
4
 
5
5
  // src/provider.tsx
6
6
 
7
+ // src/return-to.ts
8
+ var PREFIX = "oc:return-fragment:";
9
+ var FRAGMENT_TTL_MS = 15 * 60 * 1e3;
10
+ function store() {
11
+ try {
12
+ return window.sessionStorage;
13
+ } catch {
14
+ return null;
15
+ }
16
+ }
17
+ function stripFragment(target) {
18
+ const cut = target.indexOf("#");
19
+ return cut < 0 ? target : target.slice(0, cut);
20
+ }
21
+ function keep(pathAndQuery, hash) {
22
+ if (hash.length < 2) return;
23
+ try {
24
+ store()?.setItem(PREFIX + pathAndQuery, JSON.stringify({ hash, at: Date.now() }));
25
+ } catch {
26
+ }
27
+ }
28
+ function withoutFragment(target) {
29
+ if (typeof window !== "undefined" && target.includes("#")) {
30
+ try {
31
+ const u = new URL(target, window.location.href);
32
+ if (u.origin === window.location.origin) keep(u.pathname + u.search, u.hash);
33
+ } catch {
34
+ }
35
+ }
36
+ return stripFragment(target);
37
+ }
38
+ function returnToHere() {
39
+ if (typeof window === "undefined") return "/";
40
+ const { pathname, search, hash } = window.location;
41
+ return withoutFragment(pathname + search + hash);
42
+ }
43
+ function keepFragmentOnReturnLinks() {
44
+ if (typeof document === "undefined") return () => {
45
+ };
46
+ const onClick = (e) => {
47
+ const { pathname, search, hash, href } = window.location;
48
+ if (hash.length < 2 || e.defaultPrevented) return;
49
+ const link = e.target?.closest?.("a[href]");
50
+ if (!link) return;
51
+ try {
52
+ const to = new URL(link.getAttribute("href") ?? "", href);
53
+ const back = to.searchParams.get("return_to");
54
+ if (!back) return;
55
+ const here = new URL(back, href);
56
+ if (here.origin === window.location.origin && here.pathname === pathname && here.search === search) {
57
+ keep(pathname + search, hash);
58
+ }
59
+ } catch {
60
+ }
61
+ };
62
+ document.addEventListener("click", onClick, true);
63
+ return () => document.removeEventListener("click", onClick, true);
64
+ }
65
+ function restoreReturnFragment() {
66
+ if (typeof window === "undefined") return false;
67
+ const s = store();
68
+ if (!s) return false;
69
+ const { pathname, search, hash } = window.location;
70
+ const key = PREFIX + pathname + search;
71
+ let raw;
72
+ try {
73
+ raw = s.getItem(key);
74
+ if (raw === null) return false;
75
+ s.removeItem(key);
76
+ } catch {
77
+ return false;
78
+ }
79
+ if (hash) return false;
80
+ try {
81
+ const kept = JSON.parse(raw);
82
+ if (typeof kept.hash !== "string" || !kept.hash.startsWith("#")) return false;
83
+ if (typeof kept.at !== "number" || Date.now() - kept.at > FRAGMENT_TTL_MS) return false;
84
+ window.history.replaceState(window.history.state, "", pathname + search + kept.hash);
85
+ return true;
86
+ } catch {
87
+ return false;
88
+ }
89
+ }
90
+
7
91
  // src/types.ts
8
92
  var DISPLAY_IDENTITY_KINDS = ["did", "btc", "email", "nostr"];
9
93
  var DEFAULT_CONFIG = {
@@ -20,7 +104,7 @@ function buildSignInUrl(cfg, returnTo) {
20
104
  const base = `${cfg.authOrigin}${cfg.signInPath}`;
21
105
  if (!returnTo) return base;
22
106
  const u = new URL(base);
23
- u.searchParams.set("return_to", returnTo);
107
+ u.searchParams.set("return_to", stripFragment(returnTo));
24
108
  return u.toString();
25
109
  }
26
110
  function buildAddAccountUrl(cfg, returnTo) {
@@ -259,6 +343,13 @@ function OcSessionProvider({
259
343
  defaultReturnTo
260
344
  }) {
261
345
  const cfg = React4.useMemo(() => resolveConfig(config), [config]);
346
+ React4.useState(() => {
347
+ if (typeof window !== "undefined") {
348
+ consumeTabAdoptMarker();
349
+ restoreReturnFragment();
350
+ }
351
+ return null;
352
+ });
262
353
  const [account, setAccount] = React4.useState(null);
263
354
  const [roster, setRoster] = React4.useState([]);
264
355
  const [status, setStatus] = React4.useState("loading");
@@ -353,6 +444,7 @@ function OcSessionProvider({
353
444
  cancelled = true;
354
445
  };
355
446
  }, [refresh, cfg.authOrigin, cfg.multiAccount]);
447
+ React4.useEffect(() => keepFragmentOnReturnLinks(), []);
356
448
  React4.useEffect(
357
449
  () => cfg.multiAccount ? installTabFetchInterceptor(cfg.authOrigin) : void 0,
358
450
  [cfg.authOrigin, cfg.multiAccount]
@@ -849,6 +941,10 @@ function OcAccountPill({
849
941
  }
850
942
  );
851
943
  }
944
+ function accountAddress(account) {
945
+ if (account?.primaryBtc) return account.primaryBtc;
946
+ return account?.displayIdentity?.kind === "btc" ? account.displayIdentity.value : null;
947
+ }
852
948
  function useOcAddressSuggestion(options) {
853
949
  const {
854
950
  value,
@@ -858,7 +954,7 @@ function useOcAddressSuggestion(options) {
858
954
  suggestionClassName
859
955
  } = options;
860
956
  const { status, account } = useOcSession();
861
- const sessionAddress = status === "authenticated" ? account?.primaryBtc ?? null : null;
957
+ const sessionAddress = status === "authenticated" ? accountAddress(account) : null;
862
958
  const [open, setOpen] = React4.useState(false);
863
959
  const [highlighted, setHighlighted] = React4.useState(false);
864
960
  const blurTimer = React4.useRef(null);
@@ -990,7 +1086,7 @@ var OcAddressInput = React4.forwardRef(
990
1086
  ...rest
991
1087
  }, forwardedRef) {
992
1088
  const { status, account } = useOcSession();
993
- const sessionAddress = status === "authenticated" ? account?.primaryBtc ?? null : null;
1089
+ const sessionAddress = status === "authenticated" ? accountAddress(account) : null;
994
1090
  const [open, setOpen] = React4.useState(false);
995
1091
  const [highlighted, setHighlighted] = React4.useState(false);
996
1092
  const innerRef = React4.useRef(null);
@@ -1149,7 +1245,7 @@ function redirectToSudo(args = {}) {
1149
1245
  }
1150
1246
  const cfg = resolveConfig(args.config);
1151
1247
  const url = new URL("/sudo", cfg.authOrigin);
1152
- const returnTo = args.returnTo ?? window.location.href;
1248
+ const returnTo = withoutFragment(args.returnTo ?? window.location.href);
1153
1249
  if (returnTo) url.searchParams.set("return_to", returnTo);
1154
1250
  if (args.purpose) url.searchParams.set("purpose", args.purpose);
1155
1251
  window.location.assign(url.toString());
@@ -2513,7 +2609,9 @@ function ProviderSignIn({
2513
2609
  }, [authOrigin, credentials, hidden]);
2514
2610
  if (hidden) return null;
2515
2611
  if (providers !== null && providers.length === 0) return null;
2516
- const providerReturnTo = returnTo.startsWith("/") ? origin ? `${origin}${returnTo}` : returnTo : returnTo;
2612
+ const providerReturnTo = stripFragment(
2613
+ returnTo.startsWith("/") ? origin ? `${origin}${returnTo}` : returnTo : returnTo
2614
+ );
2517
2615
  const line = { flex: 1, height: 1, background: "var(--border, #27272a)" };
2518
2616
  const BLOCK_GAP = 16;
2519
2617
  const BUTTON_GAP = 8;
@@ -3878,6 +3976,6 @@ function useStepUpAuth(opts) {
3878
3976
  return { status, error, stepUp, reset };
3879
3977
  }
3880
3978
 
3881
- export { DEFAULT_CONFIG, DISPLAY_IDENTITY_KINDS, OcAccountChip, OcAccountPill, OcAddressInput, OcBond, OcLinkedIdentities, OcSessionProvider, OcSignIn, OcSignInButton, SUDO_ACCOUNT_MISMATCH_MESSAGE, TAB_ACCOUNT_HINT_KEY, TAB_ADOPT_HASH, TAB_SESSION_HEADER, TAB_SESSION_STORAGE_KEY, bondFloorsText, buildAddAccountUrl, buildSignInUrl, clearTabSession, consumeTabAccountHint, consumeTabAdoptMarker, familyReturnTarget, fetchOcBond, fetchOcLinkedIdentities, handleSudoRequired, installTabFetchInterceptor, installTabLinkDecorator, isSudoAccountMismatch, readTabSession, redirectToSudo, safeReturnTo, tabSessionHeader, useOcAddressSuggestion, useOcSession, useOptionalOcSession, useStepUpAuth, useWebAuthnList, useWebAuthnRegister, writeTabSession };
3979
+ export { DEFAULT_CONFIG, DISPLAY_IDENTITY_KINDS, FRAGMENT_TTL_MS, OcAccountChip, OcAccountPill, OcAddressInput, OcBond, OcLinkedIdentities, OcSessionProvider, OcSignIn, OcSignInButton, SUDO_ACCOUNT_MISMATCH_MESSAGE, TAB_ACCOUNT_HINT_KEY, TAB_ADOPT_HASH, TAB_SESSION_HEADER, TAB_SESSION_STORAGE_KEY, bondFloorsText, buildAddAccountUrl, buildSignInUrl, clearTabSession, consumeTabAccountHint, consumeTabAdoptMarker, familyReturnTarget, fetchOcBond, fetchOcLinkedIdentities, handleSudoRequired, installTabFetchInterceptor, installTabLinkDecorator, isSudoAccountMismatch, keepFragmentOnReturnLinks, readTabSession, redirectToSudo, restoreReturnFragment, returnToHere, safeReturnTo, stripFragment, tabSessionHeader, useOcAddressSuggestion, useOcSession, useOptionalOcSession, useStepUpAuth, useWebAuthnList, useWebAuthnRegister, withoutFragment, writeTabSession };
3882
3980
  //# sourceMappingURL=index.mjs.map
3883
3981
  //# sourceMappingURL=index.mjs.map