@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.
@@ -7,7 +7,7 @@ import {
7
7
  __configure,
8
8
  endpointRefFromApiKey,
9
9
  getRuntime
10
- } from "../chunk-AWVNDAMG.js";
10
+ } from "../chunk-I3ZMB7XM.js";
11
11
 
12
12
  // src/next/client.ts
13
13
  var SESSION_MAX_AGE_S = 2592e3;
@@ -1527,6 +1527,8 @@ var PalbeAuth = class {
1527
1527
  // suppresses AuthClient's TOKEN_REFRESHED during re-signIn
1528
1528
  signedInState = false;
1529
1529
  // dedupes AuthClient's repeated SIGNED_OUT events
1530
+ hydratingUser = false;
1531
+ // guards hydrateUser() to a single boot fetch
1530
1532
  stateListeners = /* @__PURE__ */ new Set();
1531
1533
  eventListeners = /* @__PURE__ */ new Set();
1532
1534
  userListeners = /* @__PURE__ */ new Set();
@@ -1579,13 +1581,37 @@ var PalbeAuth = class {
1579
1581
  if (changed) for (const cb of this.userListeners) this.safeInvoke(() => cb(user));
1580
1582
  return user;
1581
1583
  }
1584
+ /**
1585
+ * Restore the user after a session was rehydrated from storage (page reload).
1586
+ * Hydration in buildRuntime restores the TOKENS synchronously, but the access
1587
+ * token JWT does not carry emailVerified/createdAt, so the full AuthUser can't
1588
+ * be reconstructed offline — this fetches GET /auth/user once and announces
1589
+ * `signedIn`, so the app no longer has to `await pb.auth.refreshUser()` itself
1590
+ * on boot. Idempotent (runs once), browser-safe (no-op when not signed in or
1591
+ * a user is already cached), and never throws (a boot-time network failure
1592
+ * must not break app startup — isSignedIn stays true, the app can retry).
1593
+ */
1594
+ hydrateUser() {
1595
+ if (this.hydratingUser || this.cachedUser || !this.isSignedIn) return;
1596
+ this.hydratingUser = true;
1597
+ void this.refreshUser().then((user) => {
1598
+ if (this.isSignedIn) {
1599
+ this.signedInState = true;
1600
+ this.emitState({ status: "signedIn", user });
1601
+ }
1602
+ }).catch(() => {
1603
+ }).finally(() => {
1604
+ this.hydratingUser = false;
1605
+ });
1606
+ }
1582
1607
  // ── listeners ──────────────────────────────────────────
1583
1608
  /**
1584
1609
  * Subscribe to signed-in/signed-out state. Fires immediately with the
1585
- * current snapshot (iOS parity). NOTE: a restored session (page reload) has
1586
- * no cached user yet, so the immediate snapshot reports signedOut even when
1587
- * `isSignedIn` is true call `refreshUser()` on boot to populate the user
1588
- * and rely on `isSignedIn` for the session truth.
1610
+ * current snapshot (iOS parity). A restored session (page reload) hydrates
1611
+ * the user asynchronously via `hydrateUser()` (fired once at boot), so the
1612
+ * FIRST snapshot may report signedOut for a tick even when `isSignedIn` is
1613
+ * true; the listener then fires again with `signedIn` once the user lands.
1614
+ * Rely on `isSignedIn` for the session truth if you need it synchronously.
1589
1615
  */
1590
1616
  onAuthStateChange(callback) {
1591
1617
  this.stateListeners.add(callback);
@@ -8939,7 +8965,7 @@ function defaultSessionStorage(key) {
8939
8965
  }
8940
8966
 
8941
8967
  // src/version.ts
8942
- var VERSION = "1.8.0";
8968
+ var VERSION = "1.9.0";
8943
8969
 
8944
8970
  // src/runtime.ts
8945
8971
  function buildRuntime(config) {
@@ -9057,6 +9083,7 @@ function buildRuntime(config) {
9057
9083
  void rt.perf;
9058
9084
  if (typeof document !== "undefined") {
9059
9085
  void new PerfConfigClient().fetchConfig(rt, rt.perf);
9086
+ rt.auth.hydrateUser();
9060
9087
  }
9061
9088
  recordColdStart(rt);
9062
9089
  observeWebVitals((item) => rt.perf.record(item));