@orangecheck/auth-client 2.42.3 → 2.43.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.js CHANGED
@@ -363,6 +363,25 @@ function isTimeout(err) {
363
363
  const name = err?.name;
364
364
  return name === "TimeoutError" || name === "AbortError";
365
365
  }
366
+ async function adoptStarted(started) {
367
+ try {
368
+ const signal = timeoutSignal(ME_TIMEOUT_MS);
369
+ const res = await Promise.race([
370
+ started,
371
+ new Promise(
372
+ (_, reject) => signal.addEventListener("abort", () => reject(signal.reason), { once: true })
373
+ )
374
+ ]);
375
+ if (!res.ok) return null;
376
+ const body = await res.clone().json();
377
+ if (typeof body !== "object" || body === null || Array.isArray(body)) return null;
378
+ const account = body.account;
379
+ if (typeof account !== "object" || Array.isArray(account)) return null;
380
+ return res;
381
+ } catch {
382
+ return null;
383
+ }
384
+ }
366
385
  async function fetchHostRoster(cfg) {
367
386
  try {
368
387
  const res = await fetch(`${cfg.authOrigin}${cfg.mePath}`, {
@@ -399,7 +418,8 @@ function normalizeAccount(raw) {
399
418
  function OcSessionProvider({
400
419
  children,
401
420
  config,
402
- defaultReturnTo
421
+ defaultReturnTo,
422
+ firstRead
403
423
  }) {
404
424
  const cfg = React4__namespace.useMemo(() => resolveConfig(config), [config]);
405
425
  React4__namespace.useState(() => {
@@ -414,6 +434,7 @@ function OcSessionProvider({
414
434
  const [status, setStatus] = React4__namespace.useState("loading");
415
435
  const [error, setError] = React4__namespace.useState(null);
416
436
  const [tabPinned, setTabPinned] = React4__namespace.useState(false);
437
+ const firstReadRef = React4__namespace.useRef(firstRead);
417
438
  const pinInFlightRef = React4__namespace.useRef(false);
418
439
  const pinThisTab = React4__namespace.useCallback(
419
440
  async (didOc) => {
@@ -442,7 +463,19 @@ function OcSessionProvider({
442
463
  if (typeof window === "undefined") return;
443
464
  try {
444
465
  let pin = readTabSession();
445
- let res = await fetch(cfg.mePath, {
466
+ const take = firstReadRef.current;
467
+ firstReadRef.current = void 0;
468
+ let started = null;
469
+ try {
470
+ const got = take?.();
471
+ started = got ? Promise.resolve(got) : null;
472
+ } catch {
473
+ started = null;
474
+ }
475
+ if (started && pin) started.catch(() => void 0);
476
+ let res = (started && !pin ? await adoptStarted(started) : null) ?? // A check that never answers would leave the page loading for good;
477
+ // past the timeout it is an error, which every site already shows.
478
+ await fetch(cfg.mePath, {
446
479
  method: "GET",
447
480
  credentials: "include",
448
481
  headers: { Accept: "application/json", ...tabSessionHeader() },
@@ -2424,16 +2457,20 @@ function looksLikeBitcoinAddress(input) {
2424
2457
  return BASE58_ADDRESS.test(s);
2425
2458
  }
2426
2459
  var OAUTH_EMAIL_CODE_REASONS = /* @__PURE__ */ new Set(["email_code_required", "account_mismatch"]);
2427
- function oauthErrorText(reason, retryAt) {
2460
+ var OAUTH_PROVIDER_NAMES = /* @__PURE__ */ new Map([
2461
+ ["google", "Google"],
2462
+ ["github", "GitHub"]
2463
+ ]);
2464
+ function oauthErrorText(reason, retryAt, provider) {
2428
2465
  const cap = capText(reason, retryAt);
2429
2466
  if (cap) return cap;
2430
2467
  if (reason === "email_code_required") {
2431
2468
  return "This email already has an orangecheck account. Sign in to it with an email code below.";
2432
2469
  }
2433
2470
  if (reason === "account_mismatch") {
2434
- return "A different Google or GitHub account is already linked to this email. Use that one, or sign in with an email code below.";
2471
+ return `A different ${provider ?? "Google or GitHub"} account is already linked to this email. Use that one, or sign in with an email code below.`;
2435
2472
  }
2436
- return "That sign-in didn't finish. Try again, or use another way below.";
2473
+ return `That ${provider ? `${provider} ` : ""}sign-in didn't finish. Try again, or use another way below.`;
2437
2474
  }
2438
2475
  function OcSignIn({
2439
2476
  audience,
@@ -2500,16 +2537,18 @@ function OcSignIn({
2500
2537
  };
2501
2538
  const [signedIn, setSignedIn] = React4__namespace.useState(null);
2502
2539
  const [linkAlso, setLinkAlso] = React4__namespace.useState(false);
2503
- const [oauthError, setOauthError] = React4__namespace.useState(
2504
- null
2505
- );
2540
+ const [oauthError, setOauthError] = React4__namespace.useState(null);
2506
2541
  React4__namespace.useEffect(() => {
2507
2542
  if (typeof window === "undefined") return;
2508
2543
  const params = new URLSearchParams(window.location.search);
2509
2544
  const reason = params.get("oauth_error");
2510
2545
  if (reason === null) return;
2511
2546
  const retryAt = params.get("retry_at");
2512
- setOauthError({ reason, retryAt: retryAt === null ? null : Number(retryAt) });
2547
+ setOauthError({
2548
+ reason,
2549
+ retryAt: retryAt === null ? null : Number(retryAt),
2550
+ provider: OAUTH_PROVIDER_NAMES.get(params.get("oauth_provider") ?? "") ?? null
2551
+ });
2513
2552
  if (OAUTH_EMAIL_CODE_REASONS.has(reason) && emailEnabled) setPath("email");
2514
2553
  }, [emailEnabled]);
2515
2554
  const navigate = React4__namespace.useCallback(
@@ -2612,7 +2651,7 @@ function OcSignIn({
2612
2651
  fontFamily: SIGNIN_FONT,
2613
2652
  fontSize: signinRoleSize("text", signinSize(11))
2614
2653
  },
2615
- children: oauthErrorText(oauthError.reason, oauthError.retryAt)
2654
+ children: oauthErrorText(oauthError.reason, oauthError.retryAt, oauthError.provider)
2616
2655
  }
2617
2656
  ),
2618
2657
  addMode && /* @__PURE__ */ jsxRuntime.jsxs(