@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.mjs CHANGED
@@ -163,6 +163,8 @@ var TokenManager = class {
163
163
  this.user = null;
164
164
  // Mode: 'memory' (new backend) or 'storage' (legacy backend, default)
165
165
  this._mode = "storage";
166
+ // Callback for token changes (used by realtime to reconnect with new token)
167
+ this.onTokenChange = null;
166
168
  if (storage) {
167
169
  this.storage = storage;
168
170
  } else if (typeof window !== "undefined" && window.localStorage) {
@@ -223,12 +225,16 @@ var TokenManager = class {
223
225
  * Save session (memory always, localStorage only in storage mode)
224
226
  */
225
227
  saveSession(session) {
228
+ const tokenChanged = session.accessToken !== this.accessToken;
226
229
  this.accessToken = session.accessToken;
227
230
  this.user = session.user;
228
231
  if (this._mode === "storage") {
229
232
  this.storage.setItem(TOKEN_KEY, session.accessToken);
230
233
  this.storage.setItem(USER_KEY, JSON.stringify(session.user));
231
234
  }
235
+ if (tokenChanged && this.onTokenChange) {
236
+ this.onTokenChange();
237
+ }
232
238
  }
233
239
  /**
234
240
  * Get current session
@@ -252,10 +258,14 @@ var TokenManager = class {
252
258
  * Set access token
253
259
  */
254
260
  setAccessToken(token) {
261
+ const tokenChanged = token !== this.accessToken;
255
262
  this.accessToken = token;
256
263
  if (this._mode === "storage") {
257
264
  this.storage.setItem(TOKEN_KEY, token);
258
265
  }
266
+ if (tokenChanged && this.onTokenChange) {
267
+ this.onTokenChange();
268
+ }
259
269
  }
260
270
  /**
261
271
  * Get user
@@ -276,10 +286,14 @@ var TokenManager = class {
276
286
  * Clear session (both memory and localStorage)
277
287
  */
278
288
  clearSession() {
289
+ const hadToken = this.accessToken !== null;
279
290
  this.accessToken = null;
280
291
  this.user = null;
281
292
  this.storage.removeItem(TOKEN_KEY);
282
293
  this.storage.removeItem(USER_KEY);
294
+ if (hadToken && this.onTokenChange) {
295
+ this.onTokenChange();
296
+ }
283
297
  }
284
298
  /**
285
299
  * Check if there's a session in localStorage (for legacy detection)
@@ -1461,13 +1475,15 @@ var Functions = class {
1461
1475
  import { io } from "socket.io-client";
1462
1476
  var CONNECT_TIMEOUT = 1e4;
1463
1477
  var Realtime = class {
1464
- constructor(baseUrl, tokenManager) {
1478
+ constructor(baseUrl, tokenManager, anonKey) {
1465
1479
  this.socket = null;
1466
1480
  this.connectPromise = null;
1467
1481
  this.subscribedChannels = /* @__PURE__ */ new Set();
1468
1482
  this.eventListeners = /* @__PURE__ */ new Map();
1469
1483
  this.baseUrl = baseUrl;
1470
1484
  this.tokenManager = tokenManager;
1485
+ this.anonKey = anonKey;
1486
+ this.tokenManager.onTokenChange = () => this.onTokenChange();
1471
1487
  }
1472
1488
  notifyListeners(event, payload) {
1473
1489
  const listeners = this.eventListeners.get(event);
@@ -1493,7 +1509,7 @@ var Realtime = class {
1493
1509
  }
1494
1510
  this.connectPromise = new Promise((resolve, reject) => {
1495
1511
  const session = this.tokenManager.getSession();
1496
- const token = session?.accessToken;
1512
+ const token = session?.accessToken ?? this.anonKey;
1497
1513
  this.socket = io(this.baseUrl, {
1498
1514
  transports: ["websocket"],
1499
1515
  auth: token ? { token } : void 0
@@ -1559,6 +1575,22 @@ var Realtime = class {
1559
1575
  }
1560
1576
  this.subscribedChannels.clear();
1561
1577
  }
1578
+ /**
1579
+ * Handle token changes (e.g., after auth refresh)
1580
+ * Updates socket auth so reconnects use the new token
1581
+ * If connected, triggers reconnect to apply new token immediately
1582
+ */
1583
+ onTokenChange() {
1584
+ const session = this.tokenManager.getSession();
1585
+ const token = session?.accessToken ?? this.anonKey;
1586
+ if (this.socket) {
1587
+ this.socket.auth = token ? { token } : {};
1588
+ }
1589
+ if (this.socket?.connected) {
1590
+ this.socket.disconnect();
1591
+ this.socket.connect();
1592
+ }
1593
+ }
1562
1594
  /**
1563
1595
  * Check if connected to the realtime server
1564
1596
  */
@@ -1735,7 +1767,7 @@ var InsForgeClient = class {
1735
1767
  this.storage = new Storage(this.http);
1736
1768
  this.ai = new AI(this.http);
1737
1769
  this.functions = new Functions(this.http);
1738
- this.realtime = new Realtime(this.http.baseUrl, this.tokenManager);
1770
+ this.realtime = new Realtime(this.http.baseUrl, this.tokenManager, config.anonKey);
1739
1771
  this.emails = new Emails(this.http);
1740
1772
  }
1741
1773
  /**