@@ -20,44 +20,60 @@ qx.Class.define("osparc.CookieExpirationTracker", {
20
20
type : "singleton" ,
21
21
22
22
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
+ }
25
34
} ,
26
35
27
36
members : {
37
+ __updateInterval : null ,
28
38
__message : null ,
29
- __messageTimer : null ,
30
39
__messageInterval : null ,
31
- __logoutTimer : null ,
32
40
33
41
startTracker : function ( ) {
34
- const cookieMaxAge = osparc . store . StaticInfo . getInstance ( ) . getCookieMaxAge ( ) ;
42
+ const cookieMaxAge = osparc . store . StaticInfo . getInstance ( ) . getCookieMaxAge ( ) ; // seconds
35
43
if ( cookieMaxAge ) {
36
44
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 ) ) ;
47
47
}
48
48
} ,
49
49
50
50
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 ) ;
56
53
}
57
54
58
55
this . __removeFlashMessage ( ) ;
59
56
} ,
60
57
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 //
61
77
__displayFlashMessage : function ( willExpireIn ) {
62
78
const updateFlashMessage = ( ) => {
63
79
if ( willExpireIn <= 0 ) {
@@ -68,7 +84,8 @@ qx.Class.define("osparc.CookieExpirationTracker", {
68
84
this . __updateFlashMessage ( willExpireIn ) ;
69
85
willExpireIn -- ;
70
86
} ;
71
- this . __messageInterval = setInterval ( updateFlashMessage , 1000 ) ;
87
+ updateFlashMessage ( ) ;
88
+ this . __messageInterval = setInterval ( updateFlashMessage , 1000 ) ; // update every second
72
89
} ,
73
90
74
91
__removeFlashMessage : function ( ) {
@@ -82,15 +99,17 @@ qx.Class.define("osparc.CookieExpirationTracker", {
82
99
}
83
100
} ,
84
101
85
- __updateFlashMessage : function ( timeoutSec = 1000 ) {
102
+ __updateFlashMessage : function ( timeoutSec ) {
86
103
const timeout = osparc . utils . Utils . formatSeconds ( timeoutSec ) ;
87
104
const text = qx . locale . Manager . tr ( `Your session will expire in ${ timeout } .<br>Please log out and log in again.` ) ;
88
105
if ( this . __message === null ) {
89
106
this . __message = osparc . FlashMessenger . getInstance ( ) . logAs ( text , "WARNING" , timeoutSec * 1000 ) ;
107
+ this . __message . getChildControl ( "closebutton" ) . exclude ( ) ;
90
108
} else {
91
109
this . __message . setMessage ( text ) ;
92
110
}
93
111
} ,
112
+ // /FLASH MESSAGE //
94
113
95
114
__logoutUser : function ( ) {
96
115
const reason = qx . locale . Manager . tr ( "Session expired" ) ;
0 commit comments