From 3803b15d76521fe9a1f2e54c415f57e6ae8538fb Mon Sep 17 00:00:00 2001 From: zeripath Date: Mon, 5 Apr 2021 01:39:22 +0100 Subject: [PATCH] Drop the event source if we are unauthorized (#15275) (#15280) Backport #15275 A previous commit that sent unauthorized if the user is unauthorized simply leads to the repeated reopening of the eventsource. # This PR changes the event returned to tell the client to close the eventsource and thus prevents the repeated reopening. Signed-off-by: Andrew Thornton --- routers/events/events.go | 4 ++-- web_src/js/features/eventsource.sharedworker.js | 1 + web_src/js/features/notification.js | 5 +++++ web_src/js/features/stopwatch.js | 5 +++++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/routers/events/events.go b/routers/events/events.go index aa8e2c8c7..7542f5681 100644 --- a/routers/events/events.go +++ b/routers/events/events.go @@ -33,8 +33,8 @@ func Events(ctx *context.Context) { if !ctx.IsSigned { // Return unauthorized status event event := (&eventsource.Event{ - Name: "unauthorized", - Data: "sorry", + Name: "close", + Data: "unauthorized", }) _, _ = event.WriteTo(ctx) ctx.Resp.Flush() diff --git a/web_src/js/features/eventsource.sharedworker.js b/web_src/js/features/eventsource.sharedworker.js index a94551e82..99d92c57d 100644 --- a/web_src/js/features/eventsource.sharedworker.js +++ b/web_src/js/features/eventsource.sharedworker.js @@ -10,6 +10,7 @@ class Source { this.listening = {}; this.clients = []; this.listen('open'); + this.listen('close'); this.listen('logout'); this.listen('notification-count'); this.listen('stopwatches'); diff --git a/web_src/js/features/notification.js b/web_src/js/features/notification.js index a0793d228..fca1ddc54 100644 --- a/web_src/js/features/notification.js +++ b/web_src/js/features/notification.js @@ -74,6 +74,11 @@ export async function initNotificationCount() { }); worker.port.close(); window.location.href = AppSubUrl; + } else if (event.data.type === 'close') { + worker.port.postMessage({ + type: 'close', + }); + worker.port.close(); } }); worker.port.addEventListener('error', (e) => { diff --git a/web_src/js/features/stopwatch.js b/web_src/js/features/stopwatch.js index 61f19bd79..9352ef292 100644 --- a/web_src/js/features/stopwatch.js +++ b/web_src/js/features/stopwatch.js @@ -55,6 +55,11 @@ export async function initStopwatch() { }); worker.port.close(); window.location.href = AppSubUrl; + } else if (event.data.type === 'close') { + worker.port.postMessage({ + type: 'close', + }); + worker.port.close(); } }); worker.port.addEventListener('error', (e) => {