@timpel/forumkit-sdk-web 0.2.6 → 0.2.7

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.
@@ -392,11 +392,12 @@ function SessionProvider({ config, children }) {
392
392
  if (cancelled) return;
393
393
  expiresAtRef.current = Date.now() + expiresIn * 1e3;
394
394
  refreshTimer = setTimeout(() => {
395
- void refresh();
395
+ void refresh(true);
396
396
  }, expiresIn * 1e3 * 0.8);
397
397
  }
398
- function refresh() {
399
- return createSession2(apiUrl, config.token).then((result) => {
398
+ function refresh(isRenewal = false) {
399
+ const tokenPromise = isRenewal && config.getToken ? config.getToken() : Promise.resolve(config.token);
400
+ return tokenPromise.then((hostToken) => createSession2(apiUrl, hostToken)).then((result) => {
400
401
  if (cancelled) return;
401
402
  setState({ status: "ready", forumId: config.forumId, apiUrl, sessionToken: result.sessionToken, error: null, onLogout: config.onLogout, platform });
402
403
  scheduleRefresh(result.expiresIn);
@@ -405,7 +406,7 @@ function SessionProvider({ config, children }) {
405
406
  const message = err instanceof Error ? err.message : "Failed to create session";
406
407
  setState({ status: "error", forumId: config.forumId, apiUrl, sessionToken: null, error: message, onLogout: config.onLogout, platform });
407
408
  retryTimer = setTimeout(() => {
408
- void refresh();
409
+ void refresh(true);
409
410
  }, RETRY_DELAY_MS);
410
411
  });
411
412
  }
@@ -413,7 +414,7 @@ function SessionProvider({ config, children }) {
413
414
  if (document.visibilityState !== "visible") return;
414
415
  const msRemaining = expiresAtRef.current - Date.now();
415
416
  if (msRemaining <= 0) {
416
- void refresh();
417
+ void refresh(true);
417
418
  } else {
418
419
  if (refreshTimer) clearTimeout(refreshTimer);
419
420
  scheduleRefresh(msRemaining / 1e3);
@@ -13375,6 +13376,24 @@ var ForumKitElement = class extends HTMLElement {
13375
13376
  this._render();
13376
13377
  }
13377
13378
  }
13379
+ // Same reasoning as onLogout above - getToken is a function, so it's a JS
13380
+ // property (el.getToken = fn) rather than an observed attribute. Called by
13381
+ // SessionProvider to fetch a fresh host JWT on every renewal past the
13382
+ // first exchange, since a properly short-lived host token (the security
13383
+ // property it's meant to have) will already be expired by the time a
13384
+ // renewal is due - without this, the SDK has no way to get a new one and
13385
+ // renewal just fails once the original token expires.
13386
+ _getToken;
13387
+ get getToken() {
13388
+ return this._getToken;
13389
+ }
13390
+ set getToken(fn) {
13391
+ this._getToken = fn;
13392
+ if (this._config) {
13393
+ this._config = this._readConfig();
13394
+ this._render();
13395
+ }
13396
+ }
13378
13397
  static get observedAttributes() {
13379
13398
  return ["forum-id", "token", "theme", "api-url", "platform"];
13380
13399
  }
@@ -13424,7 +13443,8 @@ var ForumKitElement = class extends HTMLElement {
13424
13443
  theme,
13425
13444
  apiUrl,
13426
13445
  platform,
13427
- ...this._onLogout ? { onLogout: this._onLogout } : {}
13446
+ ...this._onLogout ? { onLogout: this._onLogout } : {},
13447
+ ...this._getToken ? { getToken: this._getToken } : {}
13428
13448
  };
13429
13449
  }
13430
13450
  /**
@@ -13477,4 +13497,4 @@ if (!customElements.get("forum-kit")) {
13477
13497
  export {
13478
13498
  ForumKitElement
13479
13499
  };
13480
- //# sourceMappingURL=chunk-46QQNTQZ.js.map
13500
+ //# sourceMappingURL=chunk-IFY5EDCC.js.map