@orangecheck/auth-client 2.33.1 → 2.33.3

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.d.mts CHANGED
@@ -190,6 +190,13 @@ declare function fetchOcBond(opts?: {
190
190
  }): Promise<OcBondView | null>;
191
191
  declare function OcBond({ authOrigin, onChange, className, }: OcBondProps): React.ReactElement;
192
192
 
193
+ declare const FRAGMENT_TTL_MS: number;
194
+ declare function stripFragment(target: string): string;
195
+ declare function withoutFragment(target: string): string;
196
+ declare function returnToHere(): string;
197
+ declare function keepFragmentOnReturnLinks(): () => void;
198
+ declare function restoreReturnFragment(): boolean;
199
+
193
200
  declare const TAB_SESSION_HEADER = "x-oc-tab-session";
194
201
  declare const TAB_SESSION_STORAGE_KEY = "oc_tab_session";
195
202
  declare const TAB_ACCOUNT_HINT_KEY = "oc-as";
@@ -293,4 +300,4 @@ declare function isSudoAccountMismatch(body: {
293
300
  reason?: string;
294
301
  } | null | undefined): boolean;
295
302
 
296
- export { DEFAULT_CONFIG, DISPLAY_IDENTITY_KINDS, type DisplayIdentity, type DisplayIdentityKind, type OcAccount, OcAccountChip, type OcAccountChipProps, OcAccountPill, type OcAccountPillProps, type OcAccountSummary, OcAddressInput, type OcAddressInputProps, type OcAuthConfig, OcBond, type OcBondProps, type OcBondView, OcLinkedIdentities, type OcLinkedIdentitiesProps, type OcLinkedIdentity, OcSessionProvider, type OcSessionState, type OcSessionStatus, OcSignIn, OcSignInButton, type OcSignInButtonProps, type OcSignInProps, type OcSignOutScope, type OcTabSession, type RedirectToSudoArgs, SUDO_ACCOUNT_MISMATCH_MESSAGE, TAB_ACCOUNT_HINT_KEY, TAB_ADOPT_HASH, TAB_SESSION_HEADER, TAB_SESSION_STORAGE_KEY, type UseOcAddressSuggestionOptions, type UseOcAddressSuggestionReturn, type UseStepUpAuthReturn, type UseWebAuthnListReturn, type UseWebAuthnRegisterReturn, type WebAuthnAssertionStatus, type WebAuthnCredentialPublic, type WebAuthnListStatus, type WebAuthnRegisterResult, type WebAuthnRegisterStatus, type WebAuthnRemoveResult, type WebAuthnRenameResult, type WebAuthnStepUpResult, bondFloorsText, buildAddAccountUrl, buildSignInUrl, clearTabSession, consumeTabAccountHint, consumeTabAdoptMarker, familyReturnTarget, fetchOcBond, fetchOcLinkedIdentities, handleSudoRequired, installTabFetchInterceptor, installTabLinkDecorator, isSudoAccountMismatch, readTabSession, redirectToSudo, safeReturnTo, tabSessionHeader, useOcAddressSuggestion, useOcSession, useOptionalOcSession, useStepUpAuth, useWebAuthnList, useWebAuthnRegister, writeTabSession };
303
+ export { DEFAULT_CONFIG, DISPLAY_IDENTITY_KINDS, type DisplayIdentity, type DisplayIdentityKind, FRAGMENT_TTL_MS, type OcAccount, OcAccountChip, type OcAccountChipProps, OcAccountPill, type OcAccountPillProps, type OcAccountSummary, OcAddressInput, type OcAddressInputProps, type OcAuthConfig, OcBond, type OcBondProps, type OcBondView, OcLinkedIdentities, type OcLinkedIdentitiesProps, type OcLinkedIdentity, OcSessionProvider, type OcSessionState, type OcSessionStatus, OcSignIn, OcSignInButton, type OcSignInButtonProps, type OcSignInProps, type OcSignOutScope, type OcTabSession, type RedirectToSudoArgs, SUDO_ACCOUNT_MISMATCH_MESSAGE, TAB_ACCOUNT_HINT_KEY, TAB_ADOPT_HASH, TAB_SESSION_HEADER, TAB_SESSION_STORAGE_KEY, type UseOcAddressSuggestionOptions, type UseOcAddressSuggestionReturn, type UseStepUpAuthReturn, type UseWebAuthnListReturn, type UseWebAuthnRegisterReturn, type WebAuthnAssertionStatus, type WebAuthnCredentialPublic, type WebAuthnListStatus, type WebAuthnRegisterResult, type WebAuthnRegisterStatus, type WebAuthnRemoveResult, type WebAuthnRenameResult, type WebAuthnStepUpResult, 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 };
package/dist/index.d.ts CHANGED
@@ -190,6 +190,13 @@ declare function fetchOcBond(opts?: {
190
190
  }): Promise<OcBondView | null>;
191
191
  declare function OcBond({ authOrigin, onChange, className, }: OcBondProps): React.ReactElement;
192
192
 
193
+ declare const FRAGMENT_TTL_MS: number;
194
+ declare function stripFragment(target: string): string;
195
+ declare function withoutFragment(target: string): string;
196
+ declare function returnToHere(): string;
197
+ declare function keepFragmentOnReturnLinks(): () => void;
198
+ declare function restoreReturnFragment(): boolean;
199
+
193
200
  declare const TAB_SESSION_HEADER = "x-oc-tab-session";
194
201
  declare const TAB_SESSION_STORAGE_KEY = "oc_tab_session";
195
202
  declare const TAB_ACCOUNT_HINT_KEY = "oc-as";
@@ -293,4 +300,4 @@ declare function isSudoAccountMismatch(body: {
293
300
  reason?: string;
294
301
  } | null | undefined): boolean;
295
302
 
296
- export { DEFAULT_CONFIG, DISPLAY_IDENTITY_KINDS, type DisplayIdentity, type DisplayIdentityKind, type OcAccount, OcAccountChip, type OcAccountChipProps, OcAccountPill, type OcAccountPillProps, type OcAccountSummary, OcAddressInput, type OcAddressInputProps, type OcAuthConfig, OcBond, type OcBondProps, type OcBondView, OcLinkedIdentities, type OcLinkedIdentitiesProps, type OcLinkedIdentity, OcSessionProvider, type OcSessionState, type OcSessionStatus, OcSignIn, OcSignInButton, type OcSignInButtonProps, type OcSignInProps, type OcSignOutScope, type OcTabSession, type RedirectToSudoArgs, SUDO_ACCOUNT_MISMATCH_MESSAGE, TAB_ACCOUNT_HINT_KEY, TAB_ADOPT_HASH, TAB_SESSION_HEADER, TAB_SESSION_STORAGE_KEY, type UseOcAddressSuggestionOptions, type UseOcAddressSuggestionReturn, type UseStepUpAuthReturn, type UseWebAuthnListReturn, type UseWebAuthnRegisterReturn, type WebAuthnAssertionStatus, type WebAuthnCredentialPublic, type WebAuthnListStatus, type WebAuthnRegisterResult, type WebAuthnRegisterStatus, type WebAuthnRemoveResult, type WebAuthnRenameResult, type WebAuthnStepUpResult, bondFloorsText, buildAddAccountUrl, buildSignInUrl, clearTabSession, consumeTabAccountHint, consumeTabAdoptMarker, familyReturnTarget, fetchOcBond, fetchOcLinkedIdentities, handleSudoRequired, installTabFetchInterceptor, installTabLinkDecorator, isSudoAccountMismatch, readTabSession, redirectToSudo, safeReturnTo, tabSessionHeader, useOcAddressSuggestion, useOcSession, useOptionalOcSession, useStepUpAuth, useWebAuthnList, useWebAuthnRegister, writeTabSession };
303
+ export { DEFAULT_CONFIG, DISPLAY_IDENTITY_KINDS, type DisplayIdentity, type DisplayIdentityKind, FRAGMENT_TTL_MS, type OcAccount, OcAccountChip, type OcAccountChipProps, OcAccountPill, type OcAccountPillProps, type OcAccountSummary, OcAddressInput, type OcAddressInputProps, type OcAuthConfig, OcBond, type OcBondProps, type OcBondView, OcLinkedIdentities, type OcLinkedIdentitiesProps, type OcLinkedIdentity, OcSessionProvider, type OcSessionState, type OcSessionStatus, OcSignIn, OcSignInButton, type OcSignInButtonProps, type OcSignInProps, type OcSignOutScope, type OcTabSession, type RedirectToSudoArgs, SUDO_ACCOUNT_MISMATCH_MESSAGE, TAB_ACCOUNT_HINT_KEY, TAB_ADOPT_HASH, TAB_SESSION_HEADER, TAB_SESSION_STORAGE_KEY, type UseOcAddressSuggestionOptions, type UseOcAddressSuggestionReturn, type UseStepUpAuthReturn, type UseWebAuthnListReturn, type UseWebAuthnRegisterReturn, type WebAuthnAssertionStatus, type WebAuthnCredentialPublic, type WebAuthnListStatus, type WebAuthnRegisterResult, type WebAuthnRegisterStatus, type WebAuthnRemoveResult, type WebAuthnRenameResult, type WebAuthnStepUpResult, 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 };
package/dist/index.js CHANGED
@@ -26,31 +26,6 @@ var React4__namespace = /*#__PURE__*/_interopNamespace(React4);
26
26
 
27
27
  // src/provider.tsx
28
28
 
29
- // src/types.ts
30
- var DISPLAY_IDENTITY_KINDS = ["did", "btc", "email", "nostr"];
31
- var DEFAULT_CONFIG = {
32
- authOrigin: "https://ochk.io",
33
- signInPath: "/signin",
34
- mePath: "/api/auth/me",
35
- logoutPath: "/api/auth/logout",
36
- multiAccount: true
37
- };
38
- function resolveConfig(cfg) {
39
- return { ...DEFAULT_CONFIG, ...cfg ?? {} };
40
- }
41
- function buildSignInUrl(cfg, returnTo) {
42
- const base = `${cfg.authOrigin}${cfg.signInPath}`;
43
- if (!returnTo) return base;
44
- const u = new URL(base);
45
- u.searchParams.set("return_to", returnTo);
46
- return u.toString();
47
- }
48
- function buildAddAccountUrl(cfg, returnTo) {
49
- const u = new URL(buildSignInUrl(cfg, returnTo));
50
- u.searchParams.set("add", "1");
51
- return u.toString();
52
- }
53
-
54
29
  // src/tab-session.ts
55
30
  var TAB_SESSION_HEADER = "x-oc-tab-session";
56
31
  var TAB_SESSION_STORAGE_KEY = "oc_tab_session";
@@ -222,6 +197,136 @@ async function consumeTabAccountHint(authOrigin) {
222
197
  }
223
198
  return null;
224
199
  }
200
+
201
+ // src/return-to.ts
202
+ var PREFIX = "oc:return-fragment:";
203
+ var HOLD_MS = 1e4;
204
+ var FRAGMENT_TTL_MS = 15 * 60 * 1e3;
205
+ function store() {
206
+ try {
207
+ return window.sessionStorage;
208
+ } catch {
209
+ return null;
210
+ }
211
+ }
212
+ function stripFragment(target) {
213
+ const cut = target.indexOf("#");
214
+ return cut < 0 ? target : target.slice(0, cut);
215
+ }
216
+ function keep(pathAndQuery, hash) {
217
+ if (hash.length < 2) return;
218
+ try {
219
+ store()?.setItem(PREFIX + pathAndQuery, JSON.stringify({ hash, at: Date.now() }));
220
+ } catch {
221
+ }
222
+ }
223
+ function withoutFragment(target) {
224
+ if (typeof window !== "undefined" && target.includes("#")) {
225
+ try {
226
+ const u = new URL(target, window.location.href);
227
+ if (u.origin === window.location.origin) keep(u.pathname + u.search, u.hash);
228
+ } catch {
229
+ }
230
+ }
231
+ return stripFragment(target);
232
+ }
233
+ function returnToHere() {
234
+ if (typeof window === "undefined") return "/";
235
+ const { pathname, search, hash } = window.location;
236
+ return withoutFragment(pathname + search + hash);
237
+ }
238
+ function keepFragmentOnReturnLinks() {
239
+ if (typeof document === "undefined") return () => {
240
+ };
241
+ const onClick = (e) => {
242
+ const { pathname, search, hash, href } = window.location;
243
+ if (hash.length < 2 || e.defaultPrevented) return;
244
+ const link = e.target?.closest?.("a[href]");
245
+ if (!link) return;
246
+ try {
247
+ const to = new URL(link.getAttribute("href") ?? "", href);
248
+ const back = to.searchParams.get("return_to");
249
+ if (!back) return;
250
+ const here = new URL(back, href);
251
+ if (here.origin === window.location.origin && here.pathname === pathname && here.search === search) {
252
+ keep(pathname + search, hash);
253
+ }
254
+ } catch {
255
+ }
256
+ };
257
+ document.addEventListener("click", onClick, true);
258
+ return () => document.removeEventListener("click", onClick, true);
259
+ }
260
+ function restoreReturnFragment() {
261
+ if (typeof window === "undefined") return false;
262
+ const s = store();
263
+ if (!s) return false;
264
+ const { pathname, search, hash } = window.location;
265
+ const key = PREFIX + pathname + search;
266
+ let raw;
267
+ try {
268
+ raw = s.getItem(key);
269
+ if (raw === null) return false;
270
+ s.removeItem(key);
271
+ } catch {
272
+ return false;
273
+ }
274
+ if (hash) return false;
275
+ try {
276
+ const kept = JSON.parse(raw);
277
+ if (typeof kept.hash !== "string" || !kept.hash.startsWith("#")) return false;
278
+ if (typeof kept.at !== "number" || Date.now() - kept.at > FRAGMENT_TTL_MS) return false;
279
+ const restored = pathname + search + kept.hash;
280
+ window.history.replaceState(window.history.state, "", restored);
281
+ holdFragment(pathname + search, kept.hash, restored);
282
+ return true;
283
+ } catch {
284
+ return false;
285
+ }
286
+ }
287
+ function holdFragment(pathAndQuery, hash, restored) {
288
+ const until = Date.now() + HOLD_MS;
289
+ const check = () => {
290
+ const l = window.location;
291
+ if (l.pathname + l.search !== pathAndQuery || l.hash === hash) {
292
+ if (l.pathname + l.search === pathAndQuery && Date.now() < until) {
293
+ window.setTimeout(check, 50);
294
+ }
295
+ return;
296
+ }
297
+ if (l.hash && l.hash !== TAB_ADOPT_HASH) return;
298
+ try {
299
+ window.history.replaceState(window.history.state, "", restored);
300
+ } catch {
301
+ }
302
+ };
303
+ window.setTimeout(check, 0);
304
+ }
305
+
306
+ // src/types.ts
307
+ var DISPLAY_IDENTITY_KINDS = ["did", "btc", "email", "nostr"];
308
+ var DEFAULT_CONFIG = {
309
+ authOrigin: "https://ochk.io",
310
+ signInPath: "/signin",
311
+ mePath: "/api/auth/me",
312
+ logoutPath: "/api/auth/logout",
313
+ multiAccount: true
314
+ };
315
+ function resolveConfig(cfg) {
316
+ return { ...DEFAULT_CONFIG, ...cfg ?? {} };
317
+ }
318
+ function buildSignInUrl(cfg, returnTo) {
319
+ const base = `${cfg.authOrigin}${cfg.signInPath}`;
320
+ if (!returnTo) return base;
321
+ const u = new URL(base);
322
+ u.searchParams.set("return_to", stripFragment(returnTo));
323
+ return u.toString();
324
+ }
325
+ function buildAddAccountUrl(cfg, returnTo) {
326
+ const u = new URL(buildSignInUrl(cfg, returnTo));
327
+ u.searchParams.set("add", "1");
328
+ return u.toString();
329
+ }
225
330
  var SessionContext = React4__namespace.createContext(null);
226
331
  function normalizeDisplayIdentity(raw, didOc) {
227
332
  const di = raw.display_identity ?? raw.displayIdentity;
@@ -281,6 +386,13 @@ function OcSessionProvider({
281
386
  defaultReturnTo
282
387
  }) {
283
388
  const cfg = React4__namespace.useMemo(() => resolveConfig(config), [config]);
389
+ React4__namespace.useState(() => {
390
+ if (typeof window !== "undefined") {
391
+ consumeTabAdoptMarker();
392
+ restoreReturnFragment();
393
+ }
394
+ return null;
395
+ });
284
396
  const [account, setAccount] = React4__namespace.useState(null);
285
397
  const [roster, setRoster] = React4__namespace.useState([]);
286
398
  const [status, setStatus] = React4__namespace.useState("loading");
@@ -375,6 +487,7 @@ function OcSessionProvider({
375
487
  cancelled = true;
376
488
  };
377
489
  }, [refresh, cfg.authOrigin, cfg.multiAccount]);
490
+ React4__namespace.useEffect(() => keepFragmentOnReturnLinks(), []);
378
491
  React4__namespace.useEffect(
379
492
  () => cfg.multiAccount ? installTabFetchInterceptor(cfg.authOrigin) : void 0,
380
493
  [cfg.authOrigin, cfg.multiAccount]
@@ -1175,7 +1288,7 @@ function redirectToSudo(args = {}) {
1175
1288
  }
1176
1289
  const cfg = resolveConfig(args.config);
1177
1290
  const url = new URL("/sudo", cfg.authOrigin);
1178
- const returnTo = args.returnTo ?? window.location.href;
1291
+ const returnTo = withoutFragment(args.returnTo ?? window.location.href);
1179
1292
  if (returnTo) url.searchParams.set("return_to", returnTo);
1180
1293
  if (args.purpose) url.searchParams.set("purpose", args.purpose);
1181
1294
  window.location.assign(url.toString());
@@ -2539,7 +2652,9 @@ function ProviderSignIn({
2539
2652
  }, [authOrigin, credentials, hidden]);
2540
2653
  if (hidden) return null;
2541
2654
  if (providers !== null && providers.length === 0) return null;
2542
- const providerReturnTo = returnTo.startsWith("/") ? origin ? `${origin}${returnTo}` : returnTo : returnTo;
2655
+ const providerReturnTo = stripFragment(
2656
+ returnTo.startsWith("/") ? origin ? `${origin}${returnTo}` : returnTo : returnTo
2657
+ );
2543
2658
  const line = { flex: 1, height: 1, background: "var(--border, #27272a)" };
2544
2659
  const BLOCK_GAP = 16;
2545
2660
  const BUTTON_GAP = 8;
@@ -3906,6 +4021,7 @@ function useStepUpAuth(opts) {
3906
4021
 
3907
4022
  exports.DEFAULT_CONFIG = DEFAULT_CONFIG;
3908
4023
  exports.DISPLAY_IDENTITY_KINDS = DISPLAY_IDENTITY_KINDS;
4024
+ exports.FRAGMENT_TTL_MS = FRAGMENT_TTL_MS;
3909
4025
  exports.OcAccountChip = OcAccountChip;
3910
4026
  exports.OcAccountPill = OcAccountPill;
3911
4027
  exports.OcAddressInput = OcAddressInput;
@@ -3932,9 +4048,13 @@ exports.handleSudoRequired = handleSudoRequired;
3932
4048
  exports.installTabFetchInterceptor = installTabFetchInterceptor;
3933
4049
  exports.installTabLinkDecorator = installTabLinkDecorator;
3934
4050
  exports.isSudoAccountMismatch = isSudoAccountMismatch;
4051
+ exports.keepFragmentOnReturnLinks = keepFragmentOnReturnLinks;
3935
4052
  exports.readTabSession = readTabSession;
3936
4053
  exports.redirectToSudo = redirectToSudo;
4054
+ exports.restoreReturnFragment = restoreReturnFragment;
4055
+ exports.returnToHere = returnToHere;
3937
4056
  exports.safeReturnTo = safeReturnTo;
4057
+ exports.stripFragment = stripFragment;
3938
4058
  exports.tabSessionHeader = tabSessionHeader;
3939
4059
  exports.useOcAddressSuggestion = useOcAddressSuggestion;
3940
4060
  exports.useOcSession = useOcSession;
@@ -3942,6 +4062,7 @@ exports.useOptionalOcSession = useOptionalOcSession;
3942
4062
  exports.useStepUpAuth = useStepUpAuth;
3943
4063
  exports.useWebAuthnList = useWebAuthnList;
3944
4064
  exports.useWebAuthnRegister = useWebAuthnRegister;
4065
+ exports.withoutFragment = withoutFragment;
3945
4066
  exports.writeTabSession = writeTabSession;
3946
4067
  //# sourceMappingURL=index.js.map
3947
4068
  //# sourceMappingURL=index.js.map