From bc1dec245b105349e20fc726dde423ca28bdd979 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20M=C3=A9taireau?= Date: Wed, 15 May 2024 18:56:54 +0200 Subject: [PATCH] chore(sync): Sync engine now retrieves auth token It's now it's responsability to get the authentication token from the http server and pass it to the websocket server, it will make it possible to redo the roundtrip when getting disconnected. --- umap/static/umap/js/modules/sync/engine.js | 8 +++++++- umap/static/umap/js/umap.js | 17 ++++------------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/umap/static/umap/js/modules/sync/engine.js b/umap/static/umap/js/modules/sync/engine.js index 9c4f15e6..7de5f662 100644 --- a/umap/static/umap/js/modules/sync/engine.js +++ b/umap/static/umap/js/modules/sync/engine.js @@ -5,7 +5,6 @@ export class SyncEngine { constructor(map) { this.map = map this.receiver = new MessagesDispatcher(this.map) - this._initialize() } _initialize() { @@ -15,6 +14,13 @@ export class SyncEngine { this.upsert = this.update = this.delete = noop } + async authenticate(tokenURI, webSocketURI, server) { + const [response, _, error] = await server.get(tokenURI) + if (!error) { + this.start(webSocketURI, response.token) + } + } + start(webSocketURI, authToken) { this.transport = new WebSocketTransport(webSocketURI, authToken, this.receiver) this.sender = new MessagesSender(this.transport) diff --git a/umap/static/umap/js/umap.js b/umap/static/umap/js/umap.js index cd4845c6..66b2539c 100644 --- a/umap/static/umap/js/umap.js +++ b/umap/static/umap/js/umap.js @@ -260,22 +260,13 @@ U.Map = L.Map.extend({ initSyncEngine: async function () { if (this.options.websocketEnabled == false) return - console.log('this.options.syncEnabled', this.options.syncEnabled) if (this.options.syncEnabled != true) { this.sync.stop() } else { - // FIXME: Do this directly in the sync engine, which should check if the engine - // is already started or not. - - // Get the authentication token from the server - // And pass it to the sync engine. - // FIXME: use `this.urls` - const [response, _, error] = await this.server.get( - `/map/${this.options.umap_id}/ws-token/` - ) - if (!error) { - this.sync.start(this.options.websocketURI, response.token) - } + const ws_token_uri = this.urls.get('map_websocket_auth_token', { + map_id: this.options.umap_id, + }) + await this.sync.authenticate(ws_token_uri, this.options.websocketURI, this.server) } },