@insforge/sdk 1.0.4 → 1.0.5-dev.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.
package/dist/index.js CHANGED
@@ -202,6 +202,8 @@ var TokenManager = class {
202
202
  this.user = null;
203
203
  // Mode: 'memory' (new backend) or 'storage' (legacy backend, default)
204
204
  this._mode = "storage";
205
+ // Callback for token changes (used by realtime to reconnect with new token)
206
+ this.onTokenChange = null;
205
207
  if (storage) {
206
208
  this.storage = storage;
207
209
  } else if (typeof window !== "undefined" && window.localStorage) {
@@ -262,12 +264,16 @@ var TokenManager = class {
262
264
  * Save session (memory always, localStorage only in storage mode)
263
265
  */
264
266
  saveSession(session) {
267
+ const tokenChanged = session.accessToken !== this.accessToken;
265
268
  this.accessToken = session.accessToken;
266
269
  this.user = session.user;
267
270
  if (this._mode === "storage") {
268
271
  this.storage.setItem(TOKEN_KEY, session.accessToken);
269
272
  this.storage.setItem(USER_KEY, JSON.stringify(session.user));
270
273
  }
274
+ if (tokenChanged && this.onTokenChange) {
275
+ this.onTokenChange();
276
+ }
271
277
  }
272
278
  /**
273
279
  * Get current session
@@ -291,10 +297,14 @@ var TokenManager = class {
291
297
  * Set access token
292
298
  */
293
299
  setAccessToken(token) {
300
+ const tokenChanged = token !== this.accessToken;
294
301
  this.accessToken = token;
295
302
  if (this._mode === "storage") {
296
303
  this.storage.setItem(TOKEN_KEY, token);
297
304
  }
305
+ if (tokenChanged && this.onTokenChange) {
306
+ this.onTokenChange();
307
+ }
298
308
  }
299
309
  /**
300
310
  * Get user
@@ -315,10 +325,14 @@ var TokenManager = class {
315
325
  * Clear session (both memory and localStorage)
316
326
  */
317
327
  clearSession() {
328
+ const hadToken = this.accessToken !== null;
318
329
  this.accessToken = null;
319
330
  this.user = null;
320
331
  this.storage.removeItem(TOKEN_KEY);
321
332
  this.storage.removeItem(USER_KEY);
333
+ if (hadToken && this.onTokenChange) {
334
+ this.onTokenChange();
335
+ }
322
336
  }
323
337
  /**
324
338
  * Check if there's a session in localStorage (for legacy detection)
@@ -1500,13 +1514,15 @@ var Functions = class {
1500
1514
  var import_socket = require("socket.io-client");
1501
1515
  var CONNECT_TIMEOUT = 1e4;
1502
1516
  var Realtime = class {
1503
- constructor(baseUrl, tokenManager) {
1517
+ constructor(baseUrl, tokenManager, anonKey) {
1504
1518
  this.socket = null;
1505
1519
  this.connectPromise = null;
1506
1520
  this.subscribedChannels = /* @__PURE__ */ new Set();
1507
1521
  this.eventListeners = /* @__PURE__ */ new Map();
1508
1522
  this.baseUrl = baseUrl;
1509
1523
  this.tokenManager = tokenManager;
1524
+ this.anonKey = anonKey;
1525
+ this.tokenManager.onTokenChange = () => this.onTokenChange();
1510
1526
  }
1511
1527
  notifyListeners(event, payload) {
1512
1528
  const listeners = this.eventListeners.get(event);
@@ -1532,7 +1548,7 @@ var Realtime = class {
1532
1548
  }
1533
1549
  this.connectPromise = new Promise((resolve, reject) => {
1534
1550
  const session = this.tokenManager.getSession();
1535
- const token = session?.accessToken;
1551
+ const token = session?.accessToken ?? this.anonKey;
1536
1552
  this.socket = (0, import_socket.io)(this.baseUrl, {
1537
1553
  transports: ["websocket"],
1538
1554
  auth: token ? { token } : void 0
@@ -1598,6 +1614,22 @@ var Realtime = class {
1598
1614
  }
1599
1615
  this.subscribedChannels.clear();
1600
1616
  }
1617
+ /**
1618
+ * Handle token changes (e.g., after auth refresh)
1619
+ * Updates socket auth so reconnects use the new token
1620
+ * If connected, triggers reconnect to apply new token immediately
1621
+ */
1622
+ onTokenChange() {
1623
+ const session = this.tokenManager.getSession();
1624
+ const token = session?.accessToken ?? this.anonKey;
1625
+ if (this.socket) {
1626
+ this.socket.auth = token ? { token } : {};
1627
+ }
1628
+ if (this.socket?.connected) {
1629
+ this.socket.disconnect();
1630
+ this.socket.connect();
1631
+ }
1632
+ }
1601
1633
  /**
1602
1634
  * Check if connected to the realtime server
1603
1635
  */
@@ -1774,7 +1806,7 @@ var InsForgeClient = class {
1774
1806
  this.storage = new Storage(this.http);
1775
1807
  this.ai = new AI(this.http);
1776
1808
  this.functions = new Functions(this.http);
1777
- this.realtime = new Realtime(this.http.baseUrl, this.tokenManager);
1809
+ this.realtime = new Realtime(this.http.baseUrl, this.tokenManager, config.anonKey);
1778
1810
  this.emails = new Emails(this.http);
1779
1811
  }
1780
1812
  /**