@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.cjs CHANGED
@@ -53,7 +53,8 @@ var LiveConnection = class {
53
53
  connecting: false,
54
54
  error: null,
55
55
  connectionId: null,
56
- authenticated: false
56
+ authenticated: false,
57
+ auth: { authenticated: false, session: null }
57
58
  };
58
59
  constructor(options = {}) {
59
60
  this.options = {
@@ -86,21 +87,14 @@ var LiveConnection = class {
86
87
  }
87
88
  }
88
89
  getWebSocketUrl() {
89
- const auth = this.options.auth;
90
- let baseUrl;
91
90
  if (this.options.url) {
92
- baseUrl = this.options.url;
91
+ return this.options.url;
93
92
  } else if (typeof window === "undefined") {
94
- baseUrl = "ws://localhost:3000/api/live/ws";
93
+ return "ws://localhost:3000/api/live/ws";
95
94
  } else {
96
95
  const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
97
- baseUrl = `${protocol}//${window.location.host}/api/live/ws`;
96
+ return `${protocol}//${window.location.host}/api/live/ws`;
98
97
  }
99
- if (auth?.token) {
100
- const separator = baseUrl.includes("?") ? "&" : "?";
101
- return `${baseUrl}${separator}token=${encodeURIComponent(auth.token)}`;
102
- }
103
- return baseUrl;
104
98
  }
105
99
  log(message, data) {
106
100
  if (this.options.debug) {
@@ -157,7 +151,7 @@ var LiveConnection = class {
157
151
  };
158
152
  ws.onclose = (event) => {
159
153
  this.log("Disconnected", { code: event.code, reason: event.reason });
160
- this.setState({ connected: false, connecting: false, connectionId: null });
154
+ this.setState({ connected: false, connecting: false, connectionId: null, authenticated: false, auth: { authenticated: false, session: null } });
161
155
  this.stopHeartbeat();
162
156
  if (event.code === 4003) {
163
157
  this.setState({ error: "Connection rejected: origin not allowed" });
@@ -233,17 +227,29 @@ var LiveConnection = class {
233
227
  authenticated: response.authenticated || false
234
228
  });
235
229
  const auth = this.options.auth;
236
- if (auth && !auth.token && Object.keys(auth).some((k) => auth[k])) {
230
+ if (auth && Object.keys(auth).some((k) => auth[k])) {
237
231
  this.sendMessageAndWait({ type: "AUTH", payload: auth }).then((authResp) => {
238
- if (authResp.authenticated) {
239
- this.setState({ authenticated: true });
232
+ const payload = authResp.payload;
233
+ if (payload?.authenticated) {
234
+ this.setState({
235
+ authenticated: true,
236
+ auth: { authenticated: true, session: payload.session || null }
237
+ });
240
238
  }
241
239
  }).catch(() => {
242
240
  });
243
241
  }
244
242
  }
245
243
  if (response.type === "AUTH_RESPONSE") {
246
- this.setState({ authenticated: response.authenticated || false });
244
+ const payload = response.payload;
245
+ const authenticated = payload?.authenticated || false;
246
+ this.setState({
247
+ authenticated,
248
+ auth: {
249
+ authenticated,
250
+ session: authenticated ? payload?.session || null : null
251
+ }
252
+ });
247
253
  }
248
254
  if (response.requestId && this.pendingRequests.has(response.requestId)) {
249
255
  const request = this.pendingRequests.get(response.requestId);
@@ -388,8 +394,15 @@ var LiveConnection = class {
388
394
  { type: "AUTH", payload: credentials },
389
395
  5e3
390
396
  );
391
- const success = response.authenticated || false;
392
- this.setState({ authenticated: success });
397
+ const payload = response.payload;
398
+ const success = payload?.authenticated || false;
399
+ this.setState({
400
+ authenticated: success,
401
+ auth: {
402
+ authenticated: success,
403
+ session: success ? payload?.session || null : null
404
+ }
405
+ });
393
406
  return success;
394
407
  } catch {
395
408
  return false;