@insforge/sdk 1.0.5-dev.2 → 1.0.5-dev.4

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.d.mts CHANGED
@@ -105,6 +105,7 @@ declare class TokenManager {
105
105
  private user;
106
106
  private storage;
107
107
  private _mode;
108
+ onTokenChange: (() => void) | null;
108
109
  constructor(storage?: TokenStorage);
109
110
  /**
110
111
  * Get current mode
@@ -658,7 +659,8 @@ declare class Realtime {
658
659
  private connectPromise;
659
660
  private subscribedChannels;
660
661
  private eventListeners;
661
- constructor(baseUrl: string, tokenManager: TokenManager);
662
+ private anonKey?;
663
+ constructor(baseUrl: string, tokenManager: TokenManager, anonKey?: string);
662
664
  private notifyListeners;
663
665
  /**
664
666
  * Connect to the realtime server
@@ -669,6 +671,12 @@ declare class Realtime {
669
671
  * Disconnect from the realtime server
670
672
  */
671
673
  disconnect(): void;
674
+ /**
675
+ * Handle token changes (e.g., after auth refresh)
676
+ * Updates socket auth so reconnects use the new token
677
+ * If connected, triggers reconnect to apply new token immediately
678
+ */
679
+ private onTokenChange;
672
680
  /**
673
681
  * Check if connected to the realtime server
674
682
  */
package/dist/index.d.ts CHANGED
@@ -105,6 +105,7 @@ declare class TokenManager {
105
105
  private user;
106
106
  private storage;
107
107
  private _mode;
108
+ onTokenChange: (() => void) | null;
108
109
  constructor(storage?: TokenStorage);
109
110
  /**
110
111
  * Get current mode
@@ -658,7 +659,8 @@ declare class Realtime {
658
659
  private connectPromise;
659
660
  private subscribedChannels;
660
661
  private eventListeners;
661
- constructor(baseUrl: string, tokenManager: TokenManager);
662
+ private anonKey?;
663
+ constructor(baseUrl: string, tokenManager: TokenManager, anonKey?: string);
662
664
  private notifyListeners;
663
665
  /**
664
666
  * Connect to the realtime server
@@ -669,6 +671,12 @@ declare class Realtime {
669
671
  * Disconnect from the realtime server
670
672
  */
671
673
  disconnect(): void;
674
+ /**
675
+ * Handle token changes (e.g., after auth refresh)
676
+ * Updates socket auth so reconnects use the new token
677
+ * If connected, triggers reconnect to apply new token immediately
678
+ */
679
+ private onTokenChange;
672
680
  /**
673
681
  * Check if connected to the realtime server
674
682
  */
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)
@@ -1507,13 +1521,15 @@ var Functions = class {
1507
1521
  var import_socket = require("socket.io-client");
1508
1522
  var CONNECT_TIMEOUT = 1e4;
1509
1523
  var Realtime = class {
1510
- constructor(baseUrl, tokenManager) {
1524
+ constructor(baseUrl, tokenManager, anonKey) {
1511
1525
  this.socket = null;
1512
1526
  this.connectPromise = null;
1513
1527
  this.subscribedChannels = /* @__PURE__ */ new Set();
1514
1528
  this.eventListeners = /* @__PURE__ */ new Map();
1515
1529
  this.baseUrl = baseUrl;
1516
1530
  this.tokenManager = tokenManager;
1531
+ this.anonKey = anonKey;
1532
+ this.tokenManager.onTokenChange = () => this.onTokenChange();
1517
1533
  }
1518
1534
  notifyListeners(event, payload) {
1519
1535
  const listeners = this.eventListeners.get(event);
@@ -1539,7 +1555,7 @@ var Realtime = class {
1539
1555
  }
1540
1556
  this.connectPromise = new Promise((resolve, reject) => {
1541
1557
  const session = this.tokenManager.getSession();
1542
- const token = session?.accessToken;
1558
+ const token = session?.accessToken ?? this.anonKey;
1543
1559
  this.socket = (0, import_socket.io)(this.baseUrl, {
1544
1560
  transports: ["websocket"],
1545
1561
  auth: token ? { token } : void 0
@@ -1605,6 +1621,22 @@ var Realtime = class {
1605
1621
  }
1606
1622
  this.subscribedChannels.clear();
1607
1623
  }
1624
+ /**
1625
+ * Handle token changes (e.g., after auth refresh)
1626
+ * Updates socket auth so reconnects use the new token
1627
+ * If connected, triggers reconnect to apply new token immediately
1628
+ */
1629
+ onTokenChange() {
1630
+ const session = this.tokenManager.getSession();
1631
+ const token = session?.accessToken ?? this.anonKey;
1632
+ if (this.socket) {
1633
+ this.socket.auth = token ? { token } : {};
1634
+ }
1635
+ if (this.socket?.connected) {
1636
+ this.socket.disconnect();
1637
+ this.socket.connect();
1638
+ }
1639
+ }
1608
1640
  /**
1609
1641
  * Check if connected to the realtime server
1610
1642
  */
@@ -1781,7 +1813,7 @@ var InsForgeClient = class {
1781
1813
  this.storage = new Storage(this.http);
1782
1814
  this.ai = new AI(this.http);
1783
1815
  this.functions = new Functions(this.http);
1784
- this.realtime = new Realtime(this.http.baseUrl, this.tokenManager);
1816
+ this.realtime = new Realtime(this.http.baseUrl, this.tokenManager, config.anonKey);
1785
1817
  this.emails = new Emails(this.http);
1786
1818
  }
1787
1819
  /**