@fluxstack/live-client 0.4.0 → 0.5.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.d.ts CHANGED
@@ -25,12 +25,20 @@ interface LiveConnectionOptions {
25
25
  /** Enable debug logging. Default: false */
26
26
  debug?: boolean;
27
27
  }
28
+ /** Auth state exposed to the client */
29
+ interface LiveClientAuth {
30
+ authenticated: boolean;
31
+ /** Session data from the server. Shape defined by your LiveAuthProvider. */
32
+ session: Record<string, unknown> | null;
33
+ }
28
34
  interface LiveConnectionState {
29
35
  connected: boolean;
30
36
  connecting: boolean;
31
37
  error: string | null;
32
38
  connectionId: string | null;
33
39
  authenticated: boolean;
40
+ /** Auth context with session data */
41
+ auth: LiveClientAuth;
34
42
  }
35
43
  type StateChangeCallback$1 = (state: LiveConnectionState) => void;
36
44
  type ComponentCallback = (message: WebSocketResponse) => void;
@@ -475,4 +483,4 @@ declare function onConnectionChange(callback: ConnectionStatusCallback): () => v
475
483
  */
476
484
  declare function getConnection(url?: string): LiveConnection;
477
485
 
478
- export { type AdaptiveChunkConfig, AdaptiveChunkSizer, type ChunkMetrics, type ChunkedUploadOptions, type ChunkedUploadState, ChunkedUploader, type EventHandler, type HybridState, type LiveAuthOptions, LiveComponentHandle, type LiveComponentOptions, LiveConnection, type LiveConnectionOptions, type LiveConnectionState, type PersistedState, type RoomClientMessage, type RoomHandle, RoomManager, type RoomManagerOptions, type RoomProxy, type RoomServerMessage, type StateConflict, type StateValidation, StateValidator, type Unsubscribe, type UseLiveHandle, type UseLiveOptions, clearPersistedState, createBinaryChunkMessage, getConnection, getPersistedState, onConnectionChange, persistState, useLive };
486
+ export { type AdaptiveChunkConfig, AdaptiveChunkSizer, type ChunkMetrics, type ChunkedUploadOptions, type ChunkedUploadState, ChunkedUploader, type EventHandler, type HybridState, type LiveAuthOptions, type LiveClientAuth, LiveComponentHandle, type LiveComponentOptions, LiveConnection, type LiveConnectionOptions, type LiveConnectionState, type PersistedState, type RoomClientMessage, type RoomHandle, RoomManager, type RoomManagerOptions, type RoomProxy, type RoomServerMessage, type StateConflict, type StateValidation, StateValidator, type Unsubscribe, type UseLiveHandle, type UseLiveOptions, clearPersistedState, createBinaryChunkMessage, getConnection, getPersistedState, onConnectionChange, persistState, useLive };
package/dist/index.js CHANGED
@@ -15,7 +15,8 @@ var LiveConnection = class {
15
15
  connecting: false,
16
16
  error: null,
17
17
  connectionId: null,
18
- authenticated: false
18
+ authenticated: false,
19
+ auth: { authenticated: false, session: null }
19
20
  };
20
21
  constructor(options = {}) {
21
22
  this.options = {
@@ -48,21 +49,14 @@ var LiveConnection = class {
48
49
  }
49
50
  }
50
51
  getWebSocketUrl() {
51
- const auth = this.options.auth;
52
- let baseUrl;
53
52
  if (this.options.url) {
54
- baseUrl = this.options.url;
53
+ return this.options.url;
55
54
  } else if (typeof window === "undefined") {
56
- baseUrl = "ws://localhost:3000/api/live/ws";
55
+ return "ws://localhost:3000/api/live/ws";
57
56
  } else {
58
57
  const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
59
- baseUrl = `${protocol}//${window.location.host}/api/live/ws`;
58
+ return `${protocol}//${window.location.host}/api/live/ws`;
60
59
  }
61
- if (auth?.token) {
62
- const separator = baseUrl.includes("?") ? "&" : "?";
63
- return `${baseUrl}${separator}token=${encodeURIComponent(auth.token)}`;
64
- }
65
- return baseUrl;
66
60
  }
67
61
  log(message, data) {
68
62
  if (this.options.debug) {
@@ -119,7 +113,7 @@ var LiveConnection = class {
119
113
  };
120
114
  ws.onclose = (event) => {
121
115
  this.log("Disconnected", { code: event.code, reason: event.reason });
122
- this.setState({ connected: false, connecting: false, connectionId: null });
116
+ this.setState({ connected: false, connecting: false, connectionId: null, authenticated: false, auth: { authenticated: false, session: null } });
123
117
  this.stopHeartbeat();
124
118
  if (event.code === 4003) {
125
119
  this.setState({ error: "Connection rejected: origin not allowed" });
@@ -195,17 +189,29 @@ var LiveConnection = class {
195
189
  authenticated: response.authenticated || false
196
190
  });
197
191
  const auth = this.options.auth;
198
- if (auth && !auth.token && Object.keys(auth).some((k) => auth[k])) {
192
+ if (auth && Object.keys(auth).some((k) => auth[k])) {
199
193
  this.sendMessageAndWait({ type: "AUTH", payload: auth }).then((authResp) => {
200
- if (authResp.authenticated) {
201
- this.setState({ authenticated: true });
194
+ const payload = authResp.payload;
195
+ if (payload?.authenticated) {
196
+ this.setState({
197
+ authenticated: true,
198
+ auth: { authenticated: true, session: payload.session || null }
199
+ });
202
200
  }
203
201
  }).catch(() => {
204
202
  });
205
203
  }
206
204
  }
207
205
  if (response.type === "AUTH_RESPONSE") {
208
- this.setState({ authenticated: response.authenticated || false });
206
+ const payload = response.payload;
207
+ const authenticated = payload?.authenticated || false;
208
+ this.setState({
209
+ authenticated,
210
+ auth: {
211
+ authenticated,
212
+ session: authenticated ? payload?.session || null : null
213
+ }
214
+ });
209
215
  }
210
216
  if (response.requestId && this.pendingRequests.has(response.requestId)) {
211
217
  const request = this.pendingRequests.get(response.requestId);
@@ -350,8 +356,15 @@ var LiveConnection = class {
350
356
  { type: "AUTH", payload: credentials },
351
357
  5e3
352
358
  );
353
- const success = response.authenticated || false;
354
- this.setState({ authenticated: success });
359
+ const payload = response.payload;
360
+ const success = payload?.authenticated || false;
361
+ this.setState({
362
+ authenticated: success,
363
+ auth: {
364
+ authenticated: success,
365
+ session: success ? payload?.session || null : null
366
+ }
367
+ });
355
368
  return success;
356
369
  } catch {
357
370
  return false;