Skip to content

Commit

Permalink
Drop the event source if we are unauthorized (go-gitea#15275) (go-git…
Browse files Browse the repository at this point in the history
…ea#15280)

Backport go-gitea#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>
  • Loading branch information
zeripath committed Apr 5, 2021
1 parent af73e1e commit 3803b15
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions routers/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions web_src/js/features/eventsource.sharedworker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
5 changes: 5 additions & 0 deletions web_src/js/features/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
5 changes: 5 additions & 0 deletions web_src/js/features/stopwatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 3803b15

Please sign in to comment.