1
+ class autoLogOut {
2
+ private static instance : autoLogOut ;
3
+ private _config : SignOutConfig ;
4
+ private autoSignOutTimeOut ;
5
+ private ID ;
6
+ private signOutCalled = false ;
7
+ onSignOut : ( ) => void ;
8
+
9
+ private constructor ( timeout : number = 10 , sessionKey : string = 'timestamp' , debug : boolean = false ) {
10
+ this . _config = { timeout : timeout , sessionKey : sessionKey , debug : debug } ;
11
+ this . _initHandlers ( ) ;
12
+ this . ID = Date . now ( ) ;
13
+ }
14
+
15
+ // initialization Method with configuration
16
+ static init ( config : SignOutConfig ) {
17
+ let _config : any = config || { } ;
18
+ autoLogOut . instance = new autoLogOut ( _config . timeout , _config . sessionKey , _config . debug ) ;
19
+ return autoLogOut . instance ;
20
+ }
21
+
22
+ // called OnSignOut callback
23
+ signOut ( ) {
24
+ clearTimeout ( this . autoSignOutTimeOut ) ;
25
+ this . signOutCalled = true ;
26
+ this . onSignOut ( ) ;
27
+ }
28
+
29
+ // attach handler for updating local storage
30
+ private _initHandlers ( ) {
31
+ // Add handler window unload event
32
+ window . onbeforeunload = ( ) => {
33
+ this . _extendSessionTime ( ) ;
34
+ } ;
35
+
36
+ // Add handler on document ready event
37
+ document . addEventListener ( "DOMContentLoaded" , ( ) => {
38
+ this . _extendSessionTime ( ) ;
39
+ } , false ) ;
40
+
41
+ // Add throttle event listeners
42
+ window . addEventListener ( 'mousemove' , this . throttleEvents ( ) ) ;
43
+ window . addEventListener ( 'scroll' , this . throttleEvents ( ) ) ;
44
+ window . addEventListener ( "resize" , this . throttleEvents ( ) ) ;
45
+
46
+ // Add localstorage change event
47
+ this . _addStorageEvent ( ) ;
48
+ }
49
+
50
+ // update auto logout timeout
51
+ private _setUpLogOutCalls ( ) {
52
+ if ( this . _config . debug ) {
53
+ console . log ( this . ID + ": Update the Local Storage with new value" ) ;
54
+ }
55
+ var expirationTime = this . getExpirationSessionTime ( ) ;
56
+ var currentTimestamp = Date . now ( ) ;
57
+ var difference = expirationTime - currentTimestamp ;
58
+ if ( difference <= 0 ) {
59
+ this . _logOut ( ) ;
60
+ } else {
61
+ if ( this . _config . debug ) {
62
+ console . log ( this . ID + ": Register auto logout timeout with time: " + difference ) ;
63
+ }
64
+ if ( this . autoSignOutTimeOut != null ) {
65
+ clearTimeout ( this . autoSignOutTimeOut ) ;
66
+ this . autoSignOutTimeOut = null ;
67
+ }
68
+ this . autoSignOutTimeOut = setTimeout ( ( ) => {
69
+ this . _logOut ( ) ;
70
+ } , difference ) ;
71
+ }
72
+ }
73
+
74
+ // experimental purpose: add hook on every ajax request for update localstorage
75
+ private _addHookToXMLRequest ( ) {
76
+ var oldSend ;
77
+ oldSend = XMLHttpRequest . prototype . send ;
78
+ var self = this ;
79
+ XMLHttpRequest . prototype . send = function ( ) {
80
+ self . _extendSessionTime ( ) ;
81
+ oldSend . apply ( this , arguments ) ;
82
+ }
83
+ }
84
+
85
+ // throttle Event handler
86
+ private throttleEvents ( ) {
87
+ var timeoutEvent ;
88
+ return ( ) => {
89
+ if ( timeoutEvent ) {
90
+ clearTimeout ( timeoutEvent ) ;
91
+ timeoutEvent = null ;
92
+ }
93
+ timeoutEvent = setTimeout ( ( ) => {
94
+ if ( this . _config . debug ) {
95
+ console . log ( this . ID + ": Event called" ) ;
96
+ }
97
+ this . _extendSessionTime ( ) ;
98
+ } , 100 ) ;
99
+ }
100
+ }
101
+
102
+ // localstorage change event
103
+ private _addStorageEvent ( ) {
104
+ window . addEventListener ( 'storage' , ( e ) => {
105
+ if ( e . key == this . _config . sessionKey ) {
106
+ if ( e . newValue == null ) {
107
+ return ;
108
+ }
109
+ this . _setUpLogOutCalls ( ) ;
110
+ }
111
+ } ) ;
112
+ }
113
+
114
+ // set timestamp in localstorage
115
+ private _setLocalStorageVariable ( date : Date ) {
116
+ localStorage . setItem ( this . _config . sessionKey , < any > date . getTime ( ) ) ;
117
+ localStorage . setItem ( 'date' , < any > date . toISOString ( ) ) ;
118
+ }
119
+
120
+ // extend the session timeout
121
+ private _extendSessionTime ( ) {
122
+ if ( ! this . signOutCalled ) {
123
+ var date = new Date ( ) ;
124
+ date . setMinutes ( date . getMinutes ( ) + this . _config . timeout ) ;
125
+ this . _setLocalStorageVariable ( date ) ;
126
+ this . _setUpLogOutCalls ( ) ;
127
+ }
128
+ }
129
+
130
+ // get last updated timestamp
131
+ private getExpirationSessionTime ( ) : number {
132
+ var data : number = < any > localStorage . getItem ( this . _config . sessionKey ) ;
133
+ return data || 0 ;
134
+ }
135
+
136
+ // remove item from storage
137
+ private removeFromStorage ( key : string ) {
138
+ localStorage . removeItem ( key ) ;
139
+ }
140
+
141
+ private _logOut ( ) {
142
+ if ( this . _config . debug ) {
143
+ console . log ( this . ID + ": Call SignOut" ) ;
144
+ }
145
+ this . signOutCalled = true ;
146
+ this . removeFromStorage ( this . _config . sessionKey ) ;
147
+ this . onSignOut ( ) ;
148
+ }
149
+ }
150
+
151
+ interface SignOutConfig {
152
+ timeout : number ;
153
+ sessionKey : string ;
154
+ debug : boolean ;
155
+ }
0 commit comments