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