eocene-auth-sdk 0.5.2 → 0.5.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.ts CHANGED
@@ -455,6 +455,7 @@ declare class AuthClient {
455
455
  markActive(on: boolean): void;
456
456
  private restoreLocalSession;
457
457
  private localSessionNeedsRefresh;
458
+ hasPersistedSession(): boolean;
458
459
  restoreIfNeeded(): boolean;
459
460
  restoreSession(): Promise<boolean>;
460
461
  private bootstrapRefreshToken;
package/dist/index.js CHANGED
@@ -3416,7 +3416,7 @@ var AuthClient = class {
3416
3416
  this.persist();
3417
3417
  this.active = true;
3418
3418
  this.provider = null;
3419
- this.bootstrapRefreshToken(decodeURIComponent(sessionToken));
3419
+ await this.bootstrapRefreshToken(decodeURIComponent(sessionToken));
3420
3420
  if (typeof sessionStorage !== "undefined") {
3421
3421
  sessionStorage.removeItem(this.oauthPendingKey);
3422
3422
  }
@@ -3459,6 +3459,10 @@ var AuthClient = class {
3459
3459
  const expMs = token ? sessionTokenExpMs(token) : null;
3460
3460
  return expMs == null || expMs <= Date.now() + RESTORE_REFRESH_WITHIN_MS;
3461
3461
  }
3462
+ hasPersistedSession() {
3463
+ const saved = loadPersistedState(this.storageKey);
3464
+ return Boolean(saved?.sessionToken || saved?.refreshToken);
3465
+ }
3462
3466
  restoreIfNeeded() {
3463
3467
  return this.restoreLocalSession();
3464
3468
  }
@@ -3469,23 +3473,31 @@ var AuthClient = class {
3469
3473
  });
3470
3474
  return this.restorePromise;
3471
3475
  }
3472
- bootstrapRefreshToken(sessionToken) {
3476
+ async bootstrapRefreshToken(sessionToken) {
3473
3477
  if (!this.config.projectId || !sessionToken) return;
3474
3478
  const base = resolveControlAbsoluteUrl(this.config.controlBaseUrl);
3475
- void fetch(`${base}/auth/session/refresh`, {
3476
- method: "POST",
3477
- credentials: "include",
3478
- headers: {
3479
- "content-type": "application/json",
3480
- authorization: `Bearer ${sessionToken}`
3481
- },
3482
- body: JSON.stringify({ projectId: this.config.projectId })
3483
- }).then((res) => res.ok ? res.json() : null).then((result) => {
3479
+ const controller = typeof AbortController !== "undefined" ? new AbortController() : null;
3480
+ const timer = controller ? setTimeout(() => controller.abort(), 3e3) : null;
3481
+ try {
3482
+ const res = await fetch(`${base}/auth/session/refresh`, {
3483
+ method: "POST",
3484
+ credentials: "include",
3485
+ headers: {
3486
+ "content-type": "application/json",
3487
+ authorization: `Bearer ${sessionToken}`
3488
+ },
3489
+ body: JSON.stringify({ projectId: this.config.projectId }),
3490
+ ...controller ? { signal: controller.signal } : {}
3491
+ });
3492
+ if (!res.ok) return;
3493
+ const result = await res.json().catch(() => null);
3484
3494
  if (result?.refreshToken && this.active) {
3485
3495
  this.persist(result.refreshToken);
3486
3496
  }
3487
- }).catch(() => {
3488
- });
3497
+ } catch {
3498
+ } finally {
3499
+ if (timer != null) clearTimeout(timer);
3500
+ }
3489
3501
  }
3490
3502
  async performRestoreSession() {
3491
3503
  const restoreLocalIfCurrent = () => this.restoreLocalSession() && !this.localSessionNeedsRefresh();
@@ -4684,6 +4696,12 @@ function AuthProvider({
4684
4696
  let alive = true;
4685
4697
  const c = clientRef.current;
4686
4698
  if (!c) return;
4699
+ if (!config.asDeveloper && !c.hasPersistedSession()) {
4700
+ c.restoreIfNeeded();
4701
+ setTick((n) => n + 1);
4702
+ setReady(true);
4703
+ return;
4704
+ }
4687
4705
  setReady(false);
4688
4706
  void c.restoreSession().then(() => {
4689
4707
  if (!alive || clientRef.current !== c) return;