Skip to content

Commit 50d318e

Browse files
odeimaizmrnicegyu11
authored andcommitted
🎨 [Frontend] UX Cookie Expiration Date (ITISFoundation#6066)
1 parent 12ec93d commit 50d318e

File tree

1 file changed

+41
-22
lines changed

1 file changed

+41
-22
lines changed

‎services/static-webserver/client/source/class/osparc/CookieExpirationTracker.js

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,60 @@ qx.Class.define("osparc.CookieExpirationTracker", {
2020
type: "singleton",
2121

2222
statics: {
23-
PERMANENT_WARN_IN_ADVANCE: 60*60, // Show Permanent Flash Message 1h in advance
24-
LOG_OUT_BEFORE_EXPIRING: 60 // Log user out 1' in before expiring
23+
PERMANENT_WARN_IN_ADVANCE: 2*60*60*1000, // Show Permanent Flash Message 2h in advance
24+
LOG_OUT_BEFORE_EXPIRING: 60*1000 // Log user out 1' in before expiring
25+
},
26+
27+
properties: {
28+
expirationDate: {
29+
check: "Date",
30+
nullable: false,
31+
init: null,
32+
apply: "__startInterval"
33+
}
2534
},
2635

2736
members: {
37+
__updateInterval: null,
2838
__message: null,
29-
__messageTimer: null,
3039
__messageInterval: null,
31-
__logoutTimer: null,
3240

3341
startTracker: function() {
34-
const cookieMaxAge = osparc.store.StaticInfo.getInstance().getCookieMaxAge();
42+
const cookieMaxAge = osparc.store.StaticInfo.getInstance().getCookieMaxAge(); // seconds
3543
if (cookieMaxAge) {
3644
const nowDate = new Date();
37-
const expirationTime = nowDate.getTime() + cookieMaxAge*1000 - this.self().LOG_OUT_BEFORE_EXPIRING*1000;
38-
const expirationDate = new Date(expirationTime);
39-
const showMessageIn = Math.max(cookieMaxAge - this.self().PERMANENT_WARN_IN_ADVANCE, 0);
40-
this.__messageTimer = setTimeout(() => {
41-
const willExpireIn = parseInt((expirationDate - nowDate)/1000);
42-
this.__displayFlashMessage(willExpireIn);
43-
}, showMessageIn*1000);
44-
45-
const logOutIn = Math.max(cookieMaxAge - this.self().LOG_OUT_BEFORE_EXPIRING, 0);
46-
this.__logoutTimer = setTimeout(() => this.__logoutUser(), logOutIn*1000);
45+
const expirationDateMilliseconds = nowDate.getTime() + cookieMaxAge*1000;
46+
this.setExpirationDate(new Date(expirationDateMilliseconds));
4747
}
4848
},
4949

5050
stopTracker: function() {
51-
if (this.__messageTimer) {
52-
clearTimeout(this.__messageTimer);
53-
}
54-
if (this.__logoutTimer) {
55-
clearTimeout(this.__logoutTimer);
51+
if (this.__updateInterval) {
52+
clearInterval(this.__updateInterval);
5653
}
5754

5855
this.__removeFlashMessage();
5956
},
6057

58+
__startInterval: function() {
59+
this.__checkTimes();
60+
// check every 1' if the countdown routine needs to be started
61+
this.__updateInterval = setInterval(() => this.__checkTimes(), 60*1000);
62+
},
63+
64+
__checkTimes: function() {
65+
const nowDate = new Date();
66+
const expirationDate = this.getExpirationDate();
67+
if (nowDate.getTime() + this.self().PERMANENT_WARN_IN_ADVANCE > expirationDate.getTime()) {
68+
this.__removeFlashMessage();
69+
this.__displayFlashMessage(parseInt((expirationDate.getTime() - nowDate.getTime())/1000));
70+
}
71+
if (nowDate.getTime() + this.self().LOG_OUT_BEFORE_EXPIRING > expirationDate.getTime()) {
72+
this.__logoutUser();
73+
}
74+
},
75+
76+
// FLASH MESSAGE //
6177
__displayFlashMessage: function(willExpireIn) {
6278
const updateFlashMessage = () => {
6379
if (willExpireIn <= 0) {
@@ -68,7 +84,8 @@ qx.Class.define("osparc.CookieExpirationTracker", {
6884
this.__updateFlashMessage(willExpireIn);
6985
willExpireIn--;
7086
};
71-
this.__messageInterval = setInterval(updateFlashMessage, 1000);
87+
updateFlashMessage();
88+
this.__messageInterval = setInterval(updateFlashMessage, 1000); // update every second
7289
},
7390

7491
__removeFlashMessage: function() {
@@ -82,15 +99,17 @@ qx.Class.define("osparc.CookieExpirationTracker", {
8299
}
83100
},
84101

85-
__updateFlashMessage: function(timeoutSec = 1000) {
102+
__updateFlashMessage: function(timeoutSec) {
86103
const timeout = osparc.utils.Utils.formatSeconds(timeoutSec);
87104
const text = qx.locale.Manager.tr(`Your session will expire in ${timeout}.<br>Please log out and log in again.`);
88105
if (this.__message === null) {
89106
this.__message = osparc.FlashMessenger.getInstance().logAs(text, "WARNING", timeoutSec*1000);
107+
this.__message.getChildControl("closebutton").exclude();
90108
} else {
91109
this.__message.setMessage(text);
92110
}
93111
},
112+
// /FLASH MESSAGE //
94113

95114
__logoutUser: function() {
96115
const reason = qx.locale.Manager.tr("Session expired");

0 commit comments

Comments
 (0)