@fluxstack/live-client 0.3.1 → 0.5.0

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.
@@ -56,7 +56,8 @@ var FluxstackLive = (() => {
56
56
  connecting: false,
57
57
  error: null,
58
58
  connectionId: null,
59
- authenticated: false
59
+ authenticated: false,
60
+ auth: { authenticated: false, session: null }
60
61
  });
61
62
  this.options = {
62
63
  url: options.url,
@@ -88,21 +89,14 @@ var FluxstackLive = (() => {
88
89
  }
89
90
  }
90
91
  getWebSocketUrl() {
91
- const auth = this.options.auth;
92
- let baseUrl;
93
92
  if (this.options.url) {
94
- baseUrl = this.options.url;
93
+ return this.options.url;
95
94
  } else if (typeof window === "undefined") {
96
- baseUrl = "ws://localhost:3000/api/live/ws";
95
+ return "ws://localhost:3000/api/live/ws";
97
96
  } else {
98
97
  const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
99
- baseUrl = `${protocol}//${window.location.host}/api/live/ws`;
98
+ return `${protocol}//${window.location.host}/api/live/ws`;
100
99
  }
101
- if (auth?.token) {
102
- const separator = baseUrl.includes("?") ? "&" : "?";
103
- return `${baseUrl}${separator}token=${encodeURIComponent(auth.token)}`;
104
- }
105
- return baseUrl;
106
100
  }
107
101
  log(message, data) {
108
102
  if (this.options.debug) {
@@ -159,7 +153,7 @@ var FluxstackLive = (() => {
159
153
  };
160
154
  ws.onclose = (event) => {
161
155
  this.log("Disconnected", { code: event.code, reason: event.reason });
162
- this.setState({ connected: false, connecting: false, connectionId: null });
156
+ this.setState({ connected: false, connecting: false, connectionId: null, authenticated: false, auth: { authenticated: false, session: null } });
163
157
  this.stopHeartbeat();
164
158
  if (event.code === 4003) {
165
159
  this.setState({ error: "Connection rejected: origin not allowed" });
@@ -235,17 +229,29 @@ var FluxstackLive = (() => {
235
229
  authenticated: response.authenticated || false
236
230
  });
237
231
  const auth = this.options.auth;
238
- if (auth && !auth.token && Object.keys(auth).some((k) => auth[k])) {
232
+ if (auth && Object.keys(auth).some((k) => auth[k])) {
239
233
  this.sendMessageAndWait({ type: "AUTH", payload: auth }).then((authResp) => {
240
- if (authResp.authenticated) {
241
- this.setState({ authenticated: true });
234
+ const payload = authResp.payload;
235
+ if (payload?.authenticated) {
236
+ this.setState({
237
+ authenticated: true,
238
+ auth: { authenticated: true, session: payload.session || null }
239
+ });
242
240
  }
243
241
  }).catch(() => {
244
242
  });
245
243
  }
246
244
  }
247
245
  if (response.type === "AUTH_RESPONSE") {
248
- this.setState({ authenticated: response.authenticated || false });
246
+ const payload = response.payload;
247
+ const authenticated = payload?.authenticated || false;
248
+ this.setState({
249
+ authenticated,
250
+ auth: {
251
+ authenticated,
252
+ session: authenticated ? payload?.session || null : null
253
+ }
254
+ });
249
255
  }
250
256
  if (response.requestId && this.pendingRequests.has(response.requestId)) {
251
257
  const request = this.pendingRequests.get(response.requestId);