@palbase/web 1.8.0 → 1.9.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.cjs CHANGED
@@ -489,6 +489,8 @@ var PalbeAuth = class {
489
489
  // suppresses AuthClient's TOKEN_REFRESHED during re-signIn
490
490
  signedInState = false;
491
491
  // dedupes AuthClient's repeated SIGNED_OUT events
492
+ hydratingUser = false;
493
+ // guards hydrateUser() to a single boot fetch
492
494
  stateListeners = /* @__PURE__ */ new Set();
493
495
  eventListeners = /* @__PURE__ */ new Set();
494
496
  userListeners = /* @__PURE__ */ new Set();
@@ -541,13 +543,37 @@ var PalbeAuth = class {
541
543
  if (changed) for (const cb of this.userListeners) this.safeInvoke(() => cb(user));
542
544
  return user;
543
545
  }
546
+ /**
547
+ * Restore the user after a session was rehydrated from storage (page reload).
548
+ * Hydration in buildRuntime restores the TOKENS synchronously, but the access
549
+ * token JWT does not carry emailVerified/createdAt, so the full AuthUser can't
550
+ * be reconstructed offline — this fetches GET /auth/user once and announces
551
+ * `signedIn`, so the app no longer has to `await pb.auth.refreshUser()` itself
552
+ * on boot. Idempotent (runs once), browser-safe (no-op when not signed in or
553
+ * a user is already cached), and never throws (a boot-time network failure
554
+ * must not break app startup — isSignedIn stays true, the app can retry).
555
+ */
556
+ hydrateUser() {
557
+ if (this.hydratingUser || this.cachedUser || !this.isSignedIn) return;
558
+ this.hydratingUser = true;
559
+ void this.refreshUser().then((user) => {
560
+ if (this.isSignedIn) {
561
+ this.signedInState = true;
562
+ this.emitState({ status: "signedIn", user });
563
+ }
564
+ }).catch(() => {
565
+ }).finally(() => {
566
+ this.hydratingUser = false;
567
+ });
568
+ }
544
569
  // ── listeners ──────────────────────────────────────────
545
570
  /**
546
571
  * Subscribe to signed-in/signed-out state. Fires immediately with the
547
- * current snapshot (iOS parity). NOTE: a restored session (page reload) has
548
- * no cached user yet, so the immediate snapshot reports signedOut even when
549
- * `isSignedIn` is true call `refreshUser()` on boot to populate the user
550
- * and rely on `isSignedIn` for the session truth.
572
+ * current snapshot (iOS parity). A restored session (page reload) hydrates
573
+ * the user asynchronously via `hydrateUser()` (fired once at boot), so the
574
+ * FIRST snapshot may report signedOut for a tick even when `isSignedIn` is
575
+ * true; the listener then fires again with `signedIn` once the user lands.
576
+ * Rely on `isSignedIn` for the session truth if you need it synchronously.
551
577
  */
552
578
  onAuthStateChange(callback) {
553
579
  this.stateListeners.add(callback);
@@ -7557,7 +7583,7 @@ function localStorageSessionStorage(key = DEFAULT_KEY) {
7557
7583
  }
7558
7584
 
7559
7585
  // src/version.ts
7560
- var VERSION = "1.8.0";
7586
+ var VERSION = "1.9.0";
7561
7587
 
7562
7588
  // src/internal.ts
7563
7589
  function getRuntime() {