forked from philpalmieri/jquery-idleTimeout
-
Notifications
You must be signed in to change notification settings - Fork 5
/
jquery-idleTimeout-for-testing.js
242 lines (203 loc) · 8.08 KB
/
jquery-idleTimeout-for-testing.js
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**
* This work is licensed under the Creative Commons Attribution-Share Alike 3.0
* United States License. To view a copy of this license,
* visit http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter
* to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA
*
* Configurable idle (no activity) timer and logout redirect for jQuery.
* Works cross-browser with multiple windows and tabs within the same domain.
*
* Dependencies: JQuery v1.7+, JQuery UI, store.js from https://github.com/marcuswestin/store.js - v1.3.4+
*
* Commented and console logged for debugging with Firefox & Firebug or similar
* v1.0.3
*/
(function($) {
$.fn.idleTimeout = function(options) {
console.log('start');
//##############################
//## Configuration Variables
//##############################
var defaults = {
//idleTimeLimit: 1200000, // 'No activity' time limit in milliseconds. 1200000 = 20 Minutes
idleTimeLimit: 30000, // 30 seconds for testing
//dialogDisplayLimit: 180000, // Time to display the warning dialog before redirect (and optional callback) in milliseconds. 180000 = 3 Minutes
dialogDisplayLimit: 20000, // 20 seconds for testing
redirectUrl: '/logout', // redirect to this url
// optional custom callback to perform before redirect
customCallback: false, // set to false for no customCallback
// define optional custom js function
// customCallback: function() {
// User to be logged out, perform custom action
// },
// configure which activity events to detect
// http://www.quirksmode.org/dom/events/
// https://developer.mozilla.org/en-US/docs/Web/Reference/Events
// JQuery on() method (v1.7+) expects a 'space-separated' string of event names
// activityEvents: 'click keypress scroll wheel mousewheel mousemove', // separate each event with a space
activityEvents: 'click keypress scroll wheel mousewheel', // customize events for testing - remove mousemove
//dialog box configuration
dialogTitle: 'Session Expiration Warning',
dialogText: 'Because you have been inactive, your session is about to expire.',
// server-side session keep-alive timer & url
sessionKeepAliveTimer: 60000, // Ping the server at this interval in milliseconds. 60000 = 1 Minute
// sessionKeepAliveTimer: false, // Set to false to disable pings.
sessionKeepAliveUrl: '/', // url to ping
};
//##############################
//## Private Variables
//##############################
var opts = $.extend(defaults, options);
var idleTimer, dialogTimer, idleTimerLastActivity;
var checkHeartbeat = 2000; // frequency to check for timeouts
//##############################
//## Private Functions
//##############################
// open warning dialog function
var openWarningDialog = function() {
var dialogContent = "<div id='idletimer_warning_dialog'><p>" + opts.dialogText + "</p></div>";
var warningDialog = $(dialogContent).dialog({
buttons: {
"Stay Logged In": function() {
console.log('Stay Logged In button clicked');
destroyWarningDialog();
stopDialogTimer();
startIdleTimer();
},
"Log Out Now": function() {
console.log('Log Out Now button clicked');
logoutUser();
}
},
closeOnEscape: false,
modal: true,
title: opts.dialogTitle
});
// hide the dialog's upper right corner "x" close button
$('.ui-dialog-titlebar-close').css('display', 'none');
};
// is dialog open function
var isDialogOpen = function() {
var dialogOpen = $('#idletimer_warning_dialog').dialog('isOpen');
if (dialogOpen === true) {
return true;
} else {
return false;
}
};
// destroy warning dialog function
var destroyWarningDialog = function() {
console.log('dialog destroyed');
$(".ui-dialog-content").dialog('destroy').remove();
};
// check idle timeout function
var checkIdleTimeout = function() {
var timeNow = $.now();
var timeIdleTimeout = (store.get('idleTimerLastActivity') + opts.idleTimeLimit);
if (timeNow > timeIdleTimeout) {
console.log('timeNow: ' + timeNow + ' > idle ' + timeIdleTimeout);
if (isDialogOpen() !== true) {
console.log('dialog is not open & will be opened');
openWarningDialog();
startDialogTimer();
}
} else if (store.get('idleTimerLoggedOut') === true) { //a 'manual' user logout?
logoutUser();
} else {
console.log('idle not yet timed out');
if (isDialogOpen() === true) {
console.log('dialog is open & will be closed');
destroyWarningDialog();
stopDialogTimer();
}
}
};
// start idle timer function
var startIdleTimer = function() {
stopIdleTimer();
idleTimerLastActivity = $.now();
store.set('idleTimerLastActivity', idleTimerLastActivity);
console.log('start idle timer: ' + idleTimerLastActivity);
idleTimer = setInterval(checkIdleTimeout, checkHeartbeat);
};
// stop idle timer function
var stopIdleTimer = function() {
clearInterval(idleTimer);
};
// check dialog timeout function
var checkDialogTimeout = function() {
var timeNow = $.now();
var timeDialogTimeout = (store.get('idleTimerLastActivity') + opts.idleTimeLimit + opts.dialogDisplayLimit);
if ((timeNow > timeDialogTimeout) || (store.get('idleTimerLoggedOut') === true)) {
console.log('timeNow: ' + timeNow + ' > dialog' + timeDialogTimeout);
logoutUser();
} else {
console.log('dialog not yet timed out');
}
};
// start dialog timer function
var startDialogTimer = function() {
dialogTimer = setInterval(checkDialogTimeout, checkHeartbeat);
};
// stop dialog timer function
var stopDialogTimer = function() {
clearInterval(dialogTimer);
};
// perform logout procedure function
var logoutUser = function() {
console.log('logout function');
store.set('idleTimerLoggedOut', true);
if (opts.customCallback) {
console.log('custom callback');
opts.customCallback();
}
if (opts.redirectUrl) {
window.location.href = opts.redirectUrl;
}
};
// activity detector function
// if warning dialog is NOT open, restarts idle timer
var activityDetector = function() {
$('body').on(opts.activityEvents, function() {
if (isDialogOpen() !== true) {
console.log('activity detected');
startIdleTimer();
} else {
console.log('dialog open. activity ignored');
}
});
};
// if keep-alive sessionKeepAliveTimer value is not false,
// ping the server at regular intervals to prevent a server idle timeout
var keepSessionAlive = function() {
if (opts.sessionKeepAliveTimer) {
var keepSession = function() {
// if this is the most recently active window or tab
if (idleTimerLastActivity === store.get('idleTimerLastActivity')) {
console.log('keep session alive function');
$.get(opts.sessionKeepAliveUrl);
}
};
setInterval(keepSession, opts.sessionKeepAliveTimer);
}
};
//###############################
// Build & Return the instance of the item as a plugin
// This is basically your construct.
//###############################
return this.each(function() {
console.log('instance started');
if (store.enabled) {
// initial values
idleTimerLastActivity = $.now();
store.set('idleTimerLastActivity', idleTimerLastActivity);
store.set('idleTimerLoggedOut', false);
} else {
alert('Dependent file missing. Please see: https://github.com/marcuswestin/store.js');
}
activityDetector();
keepSessionAlive();
startIdleTimer();
});
}
})(jQuery);