@insforge/sdk 1.0.5-dev.0 → 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)
@@ -1469,6 +1483,7 @@ var Realtime = class {
1469
1483
  this.baseUrl = baseUrl;
1470
1484
  this.tokenManager = tokenManager;
1471
1485
  this.anonKey = anonKey;
1486
+ this.tokenManager.onTokenChange = () => this.onTokenChange();
1472
1487
  }
1473
1488
  notifyListeners(event, payload) {
1474
1489
  const listeners = this.eventListeners.get(event);
@@ -1560,6 +1575,22 @@ var Realtime = class {
1560
1575
  }
1561
1576
  this.subscribedChannels.clear();
1562
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
+ }
1563
1594
  /**
1564
1595
  * Check if connected to the realtime server
1565
1596
  */