Drop the event source if we are unauthorized (#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 <art27@cantab.net>
release/v1.15
zeripath 2021-04-04 22:37:50 +01:00 committed by GitHub
parent f2715b8e29
commit 48ef04ee56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 2 deletions

View File

@ -33,8 +33,8 @@ func Events(ctx *context.Context) {
if !ctx.IsSigned { if !ctx.IsSigned {
// Return unauthorized status event // Return unauthorized status event
event := (&eventsource.Event{ event := (&eventsource.Event{
Name: "unauthorized", Name: "close",
Data: "sorry", Data: "unauthorized",
}) })
_, _ = event.WriteTo(ctx) _, _ = event.WriteTo(ctx)
ctx.Resp.Flush() ctx.Resp.Flush()

View File

@ -10,6 +10,7 @@ class Source {
this.listening = {}; this.listening = {};
this.clients = []; this.clients = [];
this.listen('open'); this.listen('open');
this.listen('close');
this.listen('logout'); this.listen('logout');
this.listen('notification-count'); this.listen('notification-count');
this.listen('stopwatches'); this.listen('stopwatches');

View File

@ -74,6 +74,11 @@ export async function initNotificationCount() {
}); });
worker.port.close(); worker.port.close();
window.location.href = AppSubUrl; window.location.href = AppSubUrl;
} else if (event.data.type === 'close') {
worker.port.postMessage({
type: 'close',
});
worker.port.close();
} }
}); });
worker.port.addEventListener('error', (e) => { worker.port.addEventListener('error', (e) => {

View File

@ -55,6 +55,11 @@ export async function initStopwatch() {
}); });
worker.port.close(); worker.port.close();
window.location.href = AppSubUrl; window.location.href = AppSubUrl;
} else if (event.data.type === 'close') {
worker.port.postMessage({
type: 'close',
});
worker.port.close();
} }
}); });
worker.port.addEventListener('error', (e) => { worker.port.addEventListener('error', (e) => {