@finueva/auth 0.16.0 → 0.16.1

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.
Files changed (2) hide show
  1. package/dist/index.js +30 -7
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -4472,7 +4472,7 @@ function createAuthClient(options) {
4472
4472
  baseURL: authOrigin,
4473
4473
  basePath: "/api/v1/auth",
4474
4474
  fetchOptions: {
4475
- customFetchImpl: fetchImpl,
4475
+ customFetchImpl: fetchBetterAuth,
4476
4476
  credentials: "include",
4477
4477
  retry: 0,
4478
4478
  timeout: timeoutMs
@@ -4508,6 +4508,15 @@ function createAuthClient(options) {
4508
4508
  let signalRefreshPending = false;
4509
4509
  let signalRefreshScheduled = false;
4510
4510
  let signalRefreshRequest;
4511
+ let betterAuthTransportSequence = 0;
4512
+ async function fetchBetterAuth(input, init) {
4513
+ const request = new Request(input, init);
4514
+ const url = new URL(request.url);
4515
+ if (!authStateStarted || request.method !== "GET" || url.origin !== authOrigin || url.pathname !== "/api/v1/auth/get-session" || signOutRequest || activeAccountMutationGeneration !== void 0) return fetchImpl(input, init);
4516
+ betterAuthTransportSequence += 1;
4517
+ const response = await readAuthoritySession(authorityGeneration, request);
4518
+ return new Response(JSON.stringify(response.value), { headers: { "content-type": "application/json" } });
4519
+ }
4511
4520
  function advanceAuthorityGeneration() {
4512
4521
  recentAuthenticationStartAttempt = void 0;
4513
4522
  return ++authorityGeneration;
@@ -4569,12 +4578,13 @@ function createAuthClient(options) {
4569
4578
  authoritative: false
4570
4579
  });
4571
4580
  });
4572
- getSession().catch(() => void 0);
4581
+ if (typeof window === "undefined") getSession().catch(() => void 0);
4573
4582
  }
4574
4583
  function subscribeToSessionSignals() {
4575
4584
  if (sessionSignalSubscribed) return;
4576
4585
  sessionSignalSubscribed = true;
4577
4586
  let initialized = false;
4587
+ let transportSequence = betterAuthTransportSequence;
4578
4588
  betterAuth.useSession.subscribe((state) => {
4579
4589
  const refetching = state.isRefetching === true;
4580
4590
  if (!initialized) {
@@ -4583,6 +4593,7 @@ function createAuthClient(options) {
4583
4593
  return;
4584
4594
  }
4585
4595
  const settled = betterAuthRefetching && !refetching;
4596
+ if (refetching && !betterAuthRefetching) transportSequence = betterAuthTransportSequence;
4586
4597
  betterAuthRefetching = refetching;
4587
4598
  if (refetching && signOutRequest) suppressBetterAuthSettlement = true;
4588
4599
  if (!settled) return;
@@ -4590,6 +4601,7 @@ function createAuthClient(options) {
4590
4601
  suppressBetterAuthSettlement = false;
4591
4602
  return;
4592
4603
  }
4604
+ if (transportSequence !== betterAuthTransportSequence) return;
4593
4605
  queueSignalRefresh();
4594
4606
  });
4595
4607
  }
@@ -4626,24 +4638,35 @@ function createAuthClient(options) {
4626
4638
  sessionUserId = session?.user.id;
4627
4639
  return session;
4628
4640
  }
4629
- async function refreshAuthoritySession(generation) {
4641
+ async function readAuthoritySession(generation, request) {
4630
4642
  const readEpoch = ++sessionReadEpoch;
4631
4643
  try {
4632
- const response = await requestJsonResponse(joinURL(authOrigin, "api/v1/auth/get-session"), {
4644
+ const response = await requestJsonResponse(request?.url ?? joinURL(authOrigin, "api/v1/auth/get-session"), {
4633
4645
  fetch: fetchImpl,
4634
4646
  timeoutMs,
4635
- method: "GET"
4647
+ method: "GET",
4648
+ ...request ? {
4649
+ headers: request.headers,
4650
+ signal: request.signal
4651
+ } : {}
4636
4652
  });
4637
4653
  if (generation !== authorityGeneration || readEpoch !== sessionReadEpoch) throw new AuthError("state_conflict", 409);
4638
- return acceptAuthoritySession(normalizeSession(response.value), generation);
4654
+ return {
4655
+ ...response,
4656
+ session: acceptAuthoritySession(normalizeSession(response.value), generation)
4657
+ };
4639
4658
  } catch (error) {
4640
4659
  if (generation !== authorityGeneration || readEpoch !== sessionReadEpoch) throw new AuthError("state_conflict", 409);
4641
4660
  if (error instanceof AuthError && error.code === "state_conflict") throw error;
4642
- unavailable();
4661
+ if (error instanceof AuthError && error.status === 401) acceptAuthoritySession(null, generation);
4662
+ else unavailable();
4643
4663
  if (error instanceof AuthError) throw error;
4644
4664
  throw authorityUnavailable();
4645
4665
  }
4646
4666
  }
4667
+ async function refreshAuthoritySession(generation) {
4668
+ return (await readAuthoritySession(generation)).session;
4669
+ }
4647
4670
  function getSession() {
4648
4671
  if (signOutRequest || activeAccountMutationGeneration !== void 0) return Promise.reject(new AuthError("state_conflict", 409));
4649
4672
  return refreshAuthoritySession(authorityGeneration);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@finueva/auth",
3
- "version": "0.16.0",
3
+ "version": "0.16.1",
4
4
  "description": "Public browser and server SDK for Finueva Auth",
5
5
  "type": "module",
6
6
  "sideEffects": false,