Skip to content

Commit 4e4a70c

Browse files
authored
🎨 [Frontend] React to a 401 Unauthorized (#6261)
1 parent 9de4d19 commit 4e4a70c

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

services/static-webserver/client/source/class/osparc/Application.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ qx.Class.define("osparc.Application", {
7373
const webSocket = osparc.wrapper.WebSocket.getInstance();
7474
webSocket.addListener("connect", () => osparc.WatchDog.getInstance().setOnline(true));
7575
webSocket.addListener("disconnect", () => osparc.WatchDog.getInstance().setOnline(false));
76-
webSocket.addListener("logout", () => this.logout());
76+
webSocket.addListener("logout", () => this.logout(qx.locale.Manager.tr("You were logged out")));
7777
// alert the users that they are about to navigate away
7878
// from osparc. unfortunately it is not possible
7979
// to provide our own message here
@@ -96,7 +96,7 @@ qx.Class.define("osparc.Application", {
9696
});
9797

9898
// Setting up auth manager
99-
osparc.auth.Manager.getInstance().addListener("logout", () => this.__restart(), this);
99+
osparc.auth.Manager.getInstance().addListener("loggedOut", () => this.__closeAllAndToLoginPage(), this);
100100

101101
this.__initRouting();
102102
this.__startupChecks();
@@ -491,7 +491,6 @@ qx.Class.define("osparc.Application", {
491491

492492
__restart: function() {
493493
let isLogged = osparc.auth.Manager.getInstance().isLoggedIn();
494-
495494
if (isLogged) {
496495
this.__loadMainPage();
497496
} else {
@@ -635,13 +634,21 @@ qx.Class.define("osparc.Application", {
635634
} else {
636635
osparc.FlashMessenger.getInstance().logAs(this.tr("You are logged out"), "INFO");
637636
}
637+
const isLoggedIn = osparc.auth.Manager.getInstance().isLoggedIn();
638+
if (isLoggedIn) {
639+
osparc.auth.Manager.getInstance().logout()
640+
.finally(() => this.__closeAllAndToLoginPage());
641+
} else {
642+
this.__closeAllAndToLoginPage();
643+
}
644+
},
638645

646+
__closeAllAndToLoginPage: function() {
639647
osparc.data.PollTasks.getInstance().removeTasks();
640648
osparc.MaintenanceTracker.getInstance().stopTracker();
641649
osparc.CookieExpirationTracker.getInstance().stopTracker();
642650
osparc.NewUITracker.getInstance().stopTracker();
643651
osparc.announcement.Tracker.getInstance().stopTracker();
644-
osparc.auth.Manager.getInstance().logout();
645652
if ("closeEditor" in this.__mainPage) {
646653
this.__mainPage.closeEditor();
647654
}

services/static-webserver/client/source/class/osparc/auth/Manager.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ qx.Class.define("osparc.auth.Manager", {
3333
*/
3434

3535
events: {
36-
"logout": "qx.event.type.Event"
36+
"loggedOut": "qx.event.type.Event"
3737
},
3838

3939

@@ -212,8 +212,8 @@ qx.Class.define("osparc.auth.Manager", {
212212
"client_session_id": osparc.utils.Utils.getClientSessionID()
213213
}
214214
};
215-
osparc.data.Resources.fetch("auth", "postLogout", params)
216-
.then(data => this.fireEvent("logout"))
215+
return osparc.data.Resources.fetch("auth", "postLogout", params)
216+
.then(data => this.fireEvent("loggedOut"))
217217
.catch(error => console.log("already logged out"))
218218
.finally(this.__logoutUser());
219219
},

services/static-webserver/client/source/class/osparc/dashboard/StudyBrowser.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ qx.Class.define("osparc.dashboard.StudyBrowser", {
145145
},
146146

147147
reloadResources: function() {
148-
if (osparc.data.Permissions.getInstance().canDo("studies.user.read")) {
148+
if (
149+
osparc.data.Permissions.getInstance().canDo("studies.user.read") &&
150+
osparc.auth.Manager.getInstance().isLoggedIn()
151+
) {
149152
this.__reloadStudies();
150153
} else {
151154
this.__resetStudiesList();

services/static-webserver/client/source/class/osparc/data/Resources.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757

5858
qx.Class.define("osparc.data.Resources", {
5959
extend: qx.core.Object,
60-
6160
type: "singleton",
6261

6362
defer: function(statics) {
@@ -1320,6 +1319,21 @@ qx.Class.define("osparc.data.Resources", {
13201319
status = req.getStatus();
13211320
}
13221321
res.dispose();
1322+
1323+
// If a 401 is received, make a call to the /me endpoint.
1324+
// If the backend responds with yet another 401, assume that the backend logged the user out
1325+
if (status === 401 && resource !== "profile" && osparc.auth.Manager.getInstance().isLoggedIn()) {
1326+
console.warn("Checking if user is logged in the backend");
1327+
this.fetch("profile", "getOne")
1328+
.catch(err => {
1329+
if ("status" in err && err.status === 401) {
1330+
// Unauthorized again, the cookie might have expired.
1331+
// We can assume that all calls after this will respond with 401, so bring the user ot the login page.
1332+
qx.core.Init.getApplication().logout(qx.locale.Manager.tr("You were logged out"));
1333+
}
1334+
});
1335+
}
1336+
13231337
if ([404, 503].includes(status)) {
13241338
message += "<br>Please try again later and/or contact support";
13251339
}

0 commit comments

Comments
 (0)