@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.mjs CHANGED
@@ -10,7 +10,8 @@ var DEFAULT_CONFIG = {
10
10
  authOrigin: "https://ochk.io",
11
11
  signInPath: "/signin",
12
12
  mePath: "/api/auth/me",
13
- logoutPath: "/api/auth/logout"
13
+ logoutPath: "/api/auth/logout",
14
+ multiAccount: true
14
15
  };
15
16
  function resolveConfig(cfg) {
16
17
  return { ...DEFAULT_CONFIG, ...cfg ?? {} };
@@ -266,7 +267,7 @@ function OcSessionProvider({
266
267
  const pinInFlightRef = React4.useRef(false);
267
268
  const pinThisTab = React4.useCallback(
268
269
  async (didOc) => {
269
- if (pinInFlightRef.current || readTabSession()) return;
270
+ if (!cfg.multiAccount || pinInFlightRef.current || readTabSession()) return;
270
271
  pinInFlightRef.current = true;
271
272
  try {
272
273
  const res = await fetch(`${cfg.authOrigin}/api/auth/tab`, {
@@ -285,7 +286,7 @@ function OcSessionProvider({
285
286
  pinInFlightRef.current = false;
286
287
  }
287
288
  },
288
- [cfg.authOrigin]
289
+ [cfg.authOrigin, cfg.multiAccount]
289
290
  );
290
291
  const refresh = React4.useCallback(async () => {
291
292
  if (typeof window === "undefined") return;
@@ -332,7 +333,7 @@ function OcSessionProvider({
332
333
  setError(null);
333
334
  if (acct && !pin) void pinThisTab(acct.didOc);
334
335
  const currentOrigin = typeof window !== "undefined" ? window.location.origin : null;
335
- if (acct && rosterEntries.length === 0 && currentOrigin !== null && !cfg.authOrigin.startsWith(currentOrigin)) {
336
+ if (cfg.multiAccount && acct && rosterEntries.length === 0 && currentOrigin !== null && !cfg.authOrigin.startsWith(currentOrigin)) {
336
337
  const peers = await fetchHostRoster(cfg);
337
338
  if (peers.length > 0) setRoster(peers);
338
339
  }
@@ -345,15 +346,21 @@ function OcSessionProvider({
345
346
  let cancelled = false;
346
347
  consumeTabAdoptMarker();
347
348
  void (async () => {
348
- await consumeTabAccountHint(cfg.authOrigin);
349
+ if (cfg.multiAccount) await consumeTabAccountHint(cfg.authOrigin);
349
350
  if (!cancelled) void refresh();
350
351
  })();
351
352
  return () => {
352
353
  cancelled = true;
353
354
  };
354
- }, [refresh, cfg.authOrigin]);
355
- React4.useEffect(() => installTabFetchInterceptor(cfg.authOrigin), [cfg.authOrigin]);
356
- React4.useEffect(() => installTabLinkDecorator(cfg.authOrigin), [cfg.authOrigin]);
355
+ }, [refresh, cfg.authOrigin, cfg.multiAccount]);
356
+ React4.useEffect(
357
+ () => cfg.multiAccount ? installTabFetchInterceptor(cfg.authOrigin) : void 0,
358
+ [cfg.authOrigin, cfg.multiAccount]
359
+ );
360
+ React4.useEffect(
361
+ () => cfg.multiAccount ? installTabLinkDecorator(cfg.authOrigin) : void 0,
362
+ [cfg.authOrigin, cfg.multiAccount]
363
+ );
357
364
  const signOut = React4.useCallback(
358
365
  async (opts) => {
359
366
  const scope = opts?.scope ?? "all";
@@ -2120,16 +2127,21 @@ function OcSignIn({
2120
2127
  returnTo,
2121
2128
  onSuccess,
2122
2129
  resolveReturnTo,
2123
- linkPrompt = true,
2130
+ linkPrompt: linkPromptProp = true,
2124
2131
  add: addProp,
2125
2132
  authOrigin = "https://ochk.io",
2126
2133
  initialPath,
2127
2134
  providersFirst = true,
2128
2135
  paths,
2129
- className
2136
+ className,
2137
+ mode,
2138
+ providerHref
2130
2139
  }) {
2131
2140
  const walletEnabled = paths?.wallet ?? true;
2132
2141
  const emailEnabled = paths?.email ?? true;
2142
+ const firstParty = mode === "first-party";
2143
+ const credentials = firstParty ? "omit" : "include";
2144
+ const linkPrompt = linkPromptProp && !firstParty;
2133
2145
  const [resolvedReturn, setResolvedReturn] = React4.useState(
2134
2146
  () => familyReturnTarget(returnTo) ?? safeReturnTo(returnTo)
2135
2147
  );
@@ -2144,15 +2156,19 @@ function OcSignIn({
2144
2156
  const fromQuery = familyReturnTarget(q.get("return_to")) ?? familyReturnTarget(q.get("next"));
2145
2157
  setResolvedReturn(fromQuery ?? "/");
2146
2158
  }, [returnTo]);
2147
- const [addMode, setAddMode] = React4.useState(Boolean(addProp));
2159
+ const [addMode, setAddMode] = React4.useState(Boolean(addProp) && !firstParty);
2148
2160
  React4.useEffect(() => {
2149
2161
  if (typeof window === "undefined") return;
2162
+ if (firstParty) {
2163
+ setAddMode(false);
2164
+ return;
2165
+ }
2150
2166
  if (addProp !== void 0) {
2151
2167
  setAddMode(Boolean(addProp));
2152
2168
  return;
2153
2169
  }
2154
2170
  setAddMode(new URLSearchParams(window.location.search).get("add") === "1");
2155
- }, [addProp]);
2171
+ }, [addProp, firstParty]);
2156
2172
  const [path, setPath] = React4.useState(
2157
2173
  initialPath ?? (providersFirst ? "email" : "wallet")
2158
2174
  );
@@ -2309,6 +2325,9 @@ function OcSignIn({
2309
2325
  authOrigin,
2310
2326
  returnTo: resolvedReturn,
2311
2327
  add: addMode,
2328
+ credentials,
2329
+ firstParty,
2330
+ providerHref,
2312
2331
  first: true
2313
2332
  }
2314
2333
  ),
@@ -2368,6 +2387,7 @@ function OcSignIn({
2368
2387
  authOrigin,
2369
2388
  audience,
2370
2389
  add: addMode,
2390
+ credentials,
2371
2391
  onSuccess: (a, t) => void handleSuccess(a, t, "wallet")
2372
2392
  }
2373
2393
  ),
@@ -2376,11 +2396,22 @@ function OcSignIn({
2376
2396
  {
2377
2397
  authOrigin,
2378
2398
  add: addMode,
2399
+ credentials,
2379
2400
  onSuccess: (a, t) => void handleSuccess(a, t, "email")
2380
2401
  }
2381
2402
  )
2382
2403
  ] }),
2383
- !providersFirst && /* @__PURE__ */ jsx(ProviderSignIn, { authOrigin, returnTo: resolvedReturn, add: addMode }),
2404
+ !providersFirst && /* @__PURE__ */ jsx(
2405
+ ProviderSignIn,
2406
+ {
2407
+ authOrigin,
2408
+ returnTo: resolvedReturn,
2409
+ add: addMode,
2410
+ credentials,
2411
+ firstParty,
2412
+ providerHref
2413
+ }
2414
+ ),
2384
2415
  linkPrompt && /* @__PURE__ */ jsxs("label", { "data-oc-signin-linkalso": "", style: linkAlsoStyle, children: [
2385
2416
  /* @__PURE__ */ jsx(
2386
2417
  "input",
@@ -2453,14 +2484,19 @@ function ProviderSignIn({
2453
2484
  authOrigin,
2454
2485
  returnTo,
2455
2486
  add,
2487
+ credentials,
2488
+ firstParty,
2489
+ providerHref,
2456
2490
  first = false
2457
2491
  }) {
2458
2492
  const [providers, setProviders] = React4.useState(null);
2459
2493
  const [origin, setOrigin] = React4.useState("");
2494
+ const hidden = firstParty && !providerHref;
2460
2495
  React4.useEffect(() => {
2461
2496
  setOrigin(window.location.origin);
2497
+ if (hidden) return;
2462
2498
  let cancelled = false;
2463
- fetch(`${authOrigin}/api/auth/providers`, { credentials: "include" }).then((r) => r.ok ? r.json() : null).then((body) => {
2499
+ fetch(`${authOrigin}/api/auth/providers`, { credentials }).then((r) => r.ok ? r.json() : null).then((body) => {
2464
2500
  if (!cancelled) setProviders(body?.providers ?? []);
2465
2501
  }).catch(() => {
2466
2502
  if (!cancelled) setProviders([]);
@@ -2468,7 +2504,8 @@ function ProviderSignIn({
2468
2504
  return () => {
2469
2505
  cancelled = true;
2470
2506
  };
2471
- }, [authOrigin]);
2507
+ }, [authOrigin, credentials, hidden]);
2508
+ if (hidden) return null;
2472
2509
  if (providers !== null && providers.length === 0) return null;
2473
2510
  const providerReturnTo = returnTo.startsWith("/") ? origin ? `${origin}${returnTo}` : returnTo : returnTo;
2474
2511
  const line = { flex: 1, height: 1, background: "var(--border, #27272a)" };
@@ -2526,7 +2563,7 @@ function ProviderSignIn({
2526
2563
  const buttons = providers.map((p, i) => /* @__PURE__ */ jsxs(
2527
2564
  "a",
2528
2565
  {
2529
- href: `${authOrigin}/api/auth/${p.id}/start?return_to=${encodeURIComponent(
2566
+ href: providerHref ? providerHref(p.id) : `${authOrigin}/api/auth/${p.id}/start?return_to=${encodeURIComponent(
2530
2567
  providerReturnTo
2531
2568
  )}${add ? "&add=1" : ""}`,
2532
2569
  "data-oc-signin-provider": p.id,
@@ -2559,7 +2596,13 @@ async function loadAdapter() {
2559
2596
  async function detectBrowserWallets() {
2560
2597
  return (await loadAdapter()).detectWallets().filter((w) => w.detected && w.id !== "manual").map((w) => ({ id: w.id, name: w.name }));
2561
2598
  }
2562
- function WalletFlow({ authOrigin, audience, add, onSuccess }) {
2599
+ function WalletFlow({
2600
+ authOrigin,
2601
+ audience,
2602
+ add,
2603
+ credentials,
2604
+ onSuccess
2605
+ }) {
2563
2606
  const uid = React4.useId();
2564
2607
  const ids = {
2565
2608
  address: `${uid}-address`,
@@ -2601,7 +2644,7 @@ function WalletFlow({ authOrigin, audience, add, onSuccess }) {
2601
2644
  async function submitSignature(message, nonce, signature, scheme) {
2602
2645
  const res = await fetch(`${authOrigin}/api/auth/signin`, {
2603
2646
  method: "POST",
2604
- credentials: "include",
2647
+ credentials,
2605
2648
  headers: { "Content-Type": "application/json" },
2606
2649
  body: JSON.stringify({
2607
2650
  message,
@@ -2802,7 +2845,7 @@ function WalletFlow({ authOrigin, audience, add, onSuccess }) {
2802
2845
  noBrowserWallet && /* @__PURE__ */ jsx(Hint, { children: "No browser wallet found here. You can sign with any wallet and paste." })
2803
2846
  ] });
2804
2847
  }
2805
- function EmailFlow({ authOrigin, add, onSuccess }) {
2848
+ function EmailFlow({ authOrigin, add, credentials, onSuccess }) {
2806
2849
  const uid = React4.useId();
2807
2850
  const [stage, setStage] = React4.useState("enter");
2808
2851
  const [email, setEmail] = React4.useState("");
@@ -2816,7 +2859,7 @@ function EmailFlow({ authOrigin, add, onSuccess }) {
2816
2859
  try {
2817
2860
  const res = await fetch(`${authOrigin}/api/auth/email-otp/start`, {
2818
2861
  method: "POST",
2819
- credentials: "include",
2862
+ credentials,
2820
2863
  headers: { "Content-Type": "application/json" },
2821
2864
  body: JSON.stringify({ email: address })
2822
2865
  });
@@ -2868,7 +2911,7 @@ function EmailFlow({ authOrigin, add, onSuccess }) {
2868
2911
  try {
2869
2912
  const res = await fetch(`${authOrigin}/api/auth/email-otp/verify`, {
2870
2913
  method: "POST",
2871
- credentials: "include",
2914
+ credentials,
2872
2915
  headers: { "Content-Type": "application/json" },
2873
2916
  body: JSON.stringify({
2874
2917
  email,