@orangecheck/auth-client 2.27.0 → 2.28.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/dist/index.d.mts CHANGED
@@ -47,6 +47,7 @@ interface OcAuthConfig {
47
47
  signInPath?: string;
48
48
  mePath?: string;
49
49
  logoutPath?: string;
50
+ multiAccount?: boolean;
50
51
  }
51
52
  declare const DEFAULT_CONFIG: Required<OcAuthConfig>;
52
53
  declare function buildSignInUrl(cfg: Required<OcAuthConfig>, returnTo?: string): string;
@@ -118,7 +119,7 @@ interface UseOcAddressSuggestionReturn {
118
119
  declare function useOcAddressSuggestion(options: UseOcAddressSuggestionOptions): UseOcAddressSuggestionReturn;
119
120
  declare const OcAddressInput: React.ForwardRefExoticComponent<OcAddressInputProps & React.RefAttributes<HTMLInputElement>>;
120
121
 
121
- interface OcSignInProps {
122
+ interface OcSignInBaseProps {
122
123
  audience: string;
123
124
  returnTo?: string;
124
125
  onSuccess?: (account: OcAccount, token?: string) => void;
@@ -134,9 +135,17 @@ interface OcSignInProps {
134
135
  add?: boolean;
135
136
  className?: string;
136
137
  }
138
+ type OcSignInProps = OcSignInBaseProps & ({
139
+ mode?: 'family';
140
+ providerHref?: undefined;
141
+ } | {
142
+ mode: 'first-party';
143
+ onSuccess: (account: OcAccount, token?: string) => void;
144
+ providerHref?: (providerId: string) => string;
145
+ });
137
146
  declare function safeReturnTo(input: string | undefined): string;
138
147
  declare function familyReturnTarget(input: string | undefined | null): string | undefined;
139
- declare function OcSignIn({ audience, returnTo, onSuccess, resolveReturnTo, linkPrompt, add: addProp, authOrigin, initialPath, providersFirst, paths, className, }: OcSignInProps): React.ReactElement;
148
+ declare function OcSignIn({ audience, returnTo, onSuccess, resolveReturnTo, linkPrompt: linkPromptProp, add: addProp, authOrigin, initialPath, providersFirst, paths, className, mode, providerHref, }: OcSignInProps): React.ReactElement;
140
149
 
141
150
  interface OcLinkedIdentity {
142
151
  id: string;
package/dist/index.d.ts CHANGED
@@ -47,6 +47,7 @@ interface OcAuthConfig {
47
47
  signInPath?: string;
48
48
  mePath?: string;
49
49
  logoutPath?: string;
50
+ multiAccount?: boolean;
50
51
  }
51
52
  declare const DEFAULT_CONFIG: Required<OcAuthConfig>;
52
53
  declare function buildSignInUrl(cfg: Required<OcAuthConfig>, returnTo?: string): string;
@@ -118,7 +119,7 @@ interface UseOcAddressSuggestionReturn {
118
119
  declare function useOcAddressSuggestion(options: UseOcAddressSuggestionOptions): UseOcAddressSuggestionReturn;
119
120
  declare const OcAddressInput: React.ForwardRefExoticComponent<OcAddressInputProps & React.RefAttributes<HTMLInputElement>>;
120
121
 
121
- interface OcSignInProps {
122
+ interface OcSignInBaseProps {
122
123
  audience: string;
123
124
  returnTo?: string;
124
125
  onSuccess?: (account: OcAccount, token?: string) => void;
@@ -134,9 +135,17 @@ interface OcSignInProps {
134
135
  add?: boolean;
135
136
  className?: string;
136
137
  }
138
+ type OcSignInProps = OcSignInBaseProps & ({
139
+ mode?: 'family';
140
+ providerHref?: undefined;
141
+ } | {
142
+ mode: 'first-party';
143
+ onSuccess: (account: OcAccount, token?: string) => void;
144
+ providerHref?: (providerId: string) => string;
145
+ });
137
146
  declare function safeReturnTo(input: string | undefined): string;
138
147
  declare function familyReturnTarget(input: string | undefined | null): string | undefined;
139
- declare function OcSignIn({ audience, returnTo, onSuccess, resolveReturnTo, linkPrompt, add: addProp, authOrigin, initialPath, providersFirst, paths, className, }: OcSignInProps): React.ReactElement;
148
+ declare function OcSignIn({ audience, returnTo, onSuccess, resolveReturnTo, linkPrompt: linkPromptProp, add: addProp, authOrigin, initialPath, providersFirst, paths, className, mode, providerHref, }: OcSignInProps): React.ReactElement;
140
149
 
141
150
  interface OcLinkedIdentity {
142
151
  id: string;
package/dist/index.js CHANGED
@@ -32,7 +32,8 @@ var DEFAULT_CONFIG = {
32
32
  authOrigin: "https://ochk.io",
33
33
  signInPath: "/signin",
34
34
  mePath: "/api/auth/me",
35
- logoutPath: "/api/auth/logout"
35
+ logoutPath: "/api/auth/logout",
36
+ multiAccount: true
36
37
  };
37
38
  function resolveConfig(cfg) {
38
39
  return { ...DEFAULT_CONFIG, ...cfg ?? {} };
@@ -288,7 +289,7 @@ function OcSessionProvider({
288
289
  const pinInFlightRef = React4__namespace.useRef(false);
289
290
  const pinThisTab = React4__namespace.useCallback(
290
291
  async (didOc) => {
291
- if (pinInFlightRef.current || readTabSession()) return;
292
+ if (!cfg.multiAccount || pinInFlightRef.current || readTabSession()) return;
292
293
  pinInFlightRef.current = true;
293
294
  try {
294
295
  const res = await fetch(`${cfg.authOrigin}/api/auth/tab`, {
@@ -307,7 +308,7 @@ function OcSessionProvider({
307
308
  pinInFlightRef.current = false;
308
309
  }
309
310
  },
310
- [cfg.authOrigin]
311
+ [cfg.authOrigin, cfg.multiAccount]
311
312
  );
312
313
  const refresh = React4__namespace.useCallback(async () => {
313
314
  if (typeof window === "undefined") return;
@@ -354,7 +355,7 @@ function OcSessionProvider({
354
355
  setError(null);
355
356
  if (acct && !pin) void pinThisTab(acct.didOc);
356
357
  const currentOrigin = typeof window !== "undefined" ? window.location.origin : null;
357
- if (acct && rosterEntries.length === 0 && currentOrigin !== null && !cfg.authOrigin.startsWith(currentOrigin)) {
358
+ if (cfg.multiAccount && acct && rosterEntries.length === 0 && currentOrigin !== null && !cfg.authOrigin.startsWith(currentOrigin)) {
358
359
  const peers = await fetchHostRoster(cfg);
359
360
  if (peers.length > 0) setRoster(peers);
360
361
  }
@@ -367,15 +368,21 @@ function OcSessionProvider({
367
368
  let cancelled = false;
368
369
  consumeTabAdoptMarker();
369
370
  void (async () => {
370
- await consumeTabAccountHint(cfg.authOrigin);
371
+ if (cfg.multiAccount) await consumeTabAccountHint(cfg.authOrigin);
371
372
  if (!cancelled) void refresh();
372
373
  })();
373
374
  return () => {
374
375
  cancelled = true;
375
376
  };
376
- }, [refresh, cfg.authOrigin]);
377
- React4__namespace.useEffect(() => installTabFetchInterceptor(cfg.authOrigin), [cfg.authOrigin]);
378
- React4__namespace.useEffect(() => installTabLinkDecorator(cfg.authOrigin), [cfg.authOrigin]);
377
+ }, [refresh, cfg.authOrigin, cfg.multiAccount]);
378
+ React4__namespace.useEffect(
379
+ () => cfg.multiAccount ? installTabFetchInterceptor(cfg.authOrigin) : void 0,
380
+ [cfg.authOrigin, cfg.multiAccount]
381
+ );
382
+ React4__namespace.useEffect(
383
+ () => cfg.multiAccount ? installTabLinkDecorator(cfg.authOrigin) : void 0,
384
+ [cfg.authOrigin, cfg.multiAccount]
385
+ );
379
386
  const signOut = React4__namespace.useCallback(
380
387
  async (opts) => {
381
388
  const scope = opts?.scope ?? "all";
@@ -2142,16 +2149,21 @@ function OcSignIn({
2142
2149
  returnTo,
2143
2150
  onSuccess,
2144
2151
  resolveReturnTo,
2145
- linkPrompt = true,
2152
+ linkPrompt: linkPromptProp = true,
2146
2153
  add: addProp,
2147
2154
  authOrigin = "https://ochk.io",
2148
2155
  initialPath,
2149
2156
  providersFirst = true,
2150
2157
  paths,
2151
- className
2158
+ className,
2159
+ mode,
2160
+ providerHref
2152
2161
  }) {
2153
2162
  const walletEnabled = paths?.wallet ?? true;
2154
2163
  const emailEnabled = paths?.email ?? true;
2164
+ const firstParty = mode === "first-party";
2165
+ const credentials = firstParty ? "omit" : "include";
2166
+ const linkPrompt = linkPromptProp && !firstParty;
2155
2167
  const [resolvedReturn, setResolvedReturn] = React4__namespace.useState(
2156
2168
  () => familyReturnTarget(returnTo) ?? safeReturnTo(returnTo)
2157
2169
  );
@@ -2166,15 +2178,19 @@ function OcSignIn({
2166
2178
  const fromQuery = familyReturnTarget(q.get("return_to")) ?? familyReturnTarget(q.get("next"));
2167
2179
  setResolvedReturn(fromQuery ?? "/");
2168
2180
  }, [returnTo]);
2169
- const [addMode, setAddMode] = React4__namespace.useState(Boolean(addProp));
2181
+ const [addMode, setAddMode] = React4__namespace.useState(Boolean(addProp) && !firstParty);
2170
2182
  React4__namespace.useEffect(() => {
2171
2183
  if (typeof window === "undefined") return;
2184
+ if (firstParty) {
2185
+ setAddMode(false);
2186
+ return;
2187
+ }
2172
2188
  if (addProp !== void 0) {
2173
2189
  setAddMode(Boolean(addProp));
2174
2190
  return;
2175
2191
  }
2176
2192
  setAddMode(new URLSearchParams(window.location.search).get("add") === "1");
2177
- }, [addProp]);
2193
+ }, [addProp, firstParty]);
2178
2194
  const [path, setPath] = React4__namespace.useState(
2179
2195
  initialPath ?? (providersFirst ? "email" : "wallet")
2180
2196
  );
@@ -2331,6 +2347,9 @@ function OcSignIn({
2331
2347
  authOrigin,
2332
2348
  returnTo: resolvedReturn,
2333
2349
  add: addMode,
2350
+ credentials,
2351
+ firstParty,
2352
+ providerHref,
2334
2353
  first: true
2335
2354
  }
2336
2355
  ),
@@ -2390,6 +2409,7 @@ function OcSignIn({
2390
2409
  authOrigin,
2391
2410
  audience,
2392
2411
  add: addMode,
2412
+ credentials,
2393
2413
  onSuccess: (a, t) => void handleSuccess(a, t, "wallet")
2394
2414
  }
2395
2415
  ),
@@ -2398,11 +2418,22 @@ function OcSignIn({
2398
2418
  {
2399
2419
  authOrigin,
2400
2420
  add: addMode,
2421
+ credentials,
2401
2422
  onSuccess: (a, t) => void handleSuccess(a, t, "email")
2402
2423
  }
2403
2424
  )
2404
2425
  ] }),
2405
- !providersFirst && /* @__PURE__ */ jsxRuntime.jsx(ProviderSignIn, { authOrigin, returnTo: resolvedReturn, add: addMode }),
2426
+ !providersFirst && /* @__PURE__ */ jsxRuntime.jsx(
2427
+ ProviderSignIn,
2428
+ {
2429
+ authOrigin,
2430
+ returnTo: resolvedReturn,
2431
+ add: addMode,
2432
+ credentials,
2433
+ firstParty,
2434
+ providerHref
2435
+ }
2436
+ ),
2406
2437
  linkPrompt && /* @__PURE__ */ jsxRuntime.jsxs("label", { "data-oc-signin-linkalso": "", style: linkAlsoStyle, children: [
2407
2438
  /* @__PURE__ */ jsxRuntime.jsx(
2408
2439
  "input",
@@ -2475,14 +2506,19 @@ function ProviderSignIn({
2475
2506
  authOrigin,
2476
2507
  returnTo,
2477
2508
  add,
2509
+ credentials,
2510
+ firstParty,
2511
+ providerHref,
2478
2512
  first = false
2479
2513
  }) {
2480
2514
  const [providers, setProviders] = React4__namespace.useState(null);
2481
2515
  const [origin, setOrigin] = React4__namespace.useState("");
2516
+ const hidden = firstParty && !providerHref;
2482
2517
  React4__namespace.useEffect(() => {
2483
2518
  setOrigin(window.location.origin);
2519
+ if (hidden) return;
2484
2520
  let cancelled = false;
2485
- fetch(`${authOrigin}/api/auth/providers`, { credentials: "include" }).then((r) => r.ok ? r.json() : null).then((body) => {
2521
+ fetch(`${authOrigin}/api/auth/providers`, { credentials }).then((r) => r.ok ? r.json() : null).then((body) => {
2486
2522
  if (!cancelled) setProviders(body?.providers ?? []);
2487
2523
  }).catch(() => {
2488
2524
  if (!cancelled) setProviders([]);
@@ -2490,7 +2526,8 @@ function ProviderSignIn({
2490
2526
  return () => {
2491
2527
  cancelled = true;
2492
2528
  };
2493
- }, [authOrigin]);
2529
+ }, [authOrigin, credentials, hidden]);
2530
+ if (hidden) return null;
2494
2531
  if (providers !== null && providers.length === 0) return null;
2495
2532
  const providerReturnTo = returnTo.startsWith("/") ? origin ? `${origin}${returnTo}` : returnTo : returnTo;
2496
2533
  const line = { flex: 1, height: 1, background: "var(--border, #27272a)" };
@@ -2548,7 +2585,7 @@ function ProviderSignIn({
2548
2585
  const buttons = providers.map((p, i) => /* @__PURE__ */ jsxRuntime.jsxs(
2549
2586
  "a",
2550
2587
  {
2551
- href: `${authOrigin}/api/auth/${p.id}/start?return_to=${encodeURIComponent(
2588
+ href: providerHref ? providerHref(p.id) : `${authOrigin}/api/auth/${p.id}/start?return_to=${encodeURIComponent(
2552
2589
  providerReturnTo
2553
2590
  )}${add ? "&add=1" : ""}`,
2554
2591
  "data-oc-signin-provider": p.id,
@@ -2581,7 +2618,13 @@ async function loadAdapter() {
2581
2618
  async function detectBrowserWallets() {
2582
2619
  return (await loadAdapter()).detectWallets().filter((w) => w.detected && w.id !== "manual").map((w) => ({ id: w.id, name: w.name }));
2583
2620
  }
2584
- function WalletFlow({ authOrigin, audience, add, onSuccess }) {
2621
+ function WalletFlow({
2622
+ authOrigin,
2623
+ audience,
2624
+ add,
2625
+ credentials,
2626
+ onSuccess
2627
+ }) {
2585
2628
  const uid = React4__namespace.useId();
2586
2629
  const ids = {
2587
2630
  address: `${uid}-address`,
@@ -2623,7 +2666,7 @@ function WalletFlow({ authOrigin, audience, add, onSuccess }) {
2623
2666
  async function submitSignature(message, nonce, signature, scheme) {
2624
2667
  const res = await fetch(`${authOrigin}/api/auth/signin`, {
2625
2668
  method: "POST",
2626
- credentials: "include",
2669
+ credentials,
2627
2670
  headers: { "Content-Type": "application/json" },
2628
2671
  body: JSON.stringify({
2629
2672
  message,
@@ -2824,7 +2867,7 @@ function WalletFlow({ authOrigin, audience, add, onSuccess }) {
2824
2867
  noBrowserWallet && /* @__PURE__ */ jsxRuntime.jsx(Hint, { children: "No browser wallet found here. You can sign with any wallet and paste." })
2825
2868
  ] });
2826
2869
  }
2827
- function EmailFlow({ authOrigin, add, onSuccess }) {
2870
+ function EmailFlow({ authOrigin, add, credentials, onSuccess }) {
2828
2871
  const uid = React4__namespace.useId();
2829
2872
  const [stage, setStage] = React4__namespace.useState("enter");
2830
2873
  const [email, setEmail] = React4__namespace.useState("");
@@ -2838,7 +2881,7 @@ function EmailFlow({ authOrigin, add, onSuccess }) {
2838
2881
  try {
2839
2882
  const res = await fetch(`${authOrigin}/api/auth/email-otp/start`, {
2840
2883
  method: "POST",
2841
- credentials: "include",
2884
+ credentials,
2842
2885
  headers: { "Content-Type": "application/json" },
2843
2886
  body: JSON.stringify({ email: address })
2844
2887
  });
@@ -2890,7 +2933,7 @@ function EmailFlow({ authOrigin, add, onSuccess }) {
2890
2933
  try {
2891
2934
  const res = await fetch(`${authOrigin}/api/auth/email-otp/verify`, {
2892
2935
  method: "POST",
2893
- credentials: "include",
2936
+ credentials,
2894
2937
  headers: { "Content-Type": "application/json" },
2895
2938
  body: JSON.stringify({
2896
2939
  email,