Skip to content

Commit 4776514

Browse files
committed
Initial Release
0 parents  commit 4776514

8 files changed

Lines changed: 413 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules/

.jshintrc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"curly": true,
3+
"eqeqeq": true,
4+
"immed": true,
5+
"latedef": true,
6+
"newcap": true,
7+
"noarg": true,
8+
"sub": true,
9+
"undef": true,
10+
"unused": true,
11+
"boss": true,
12+
"eqnull": true,
13+
"node": true,
14+
"globals": {
15+
"document": true,
16+
"window": true,
17+
"jQuery": true
18+
}
19+
}

Gruntfile.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
3+
module.exports = function (grunt) {
4+
// Show elapsed time at the end
5+
require('time-grunt')(grunt);
6+
// Load all grunt tasks
7+
require('load-grunt-tasks')(grunt);
8+
9+
// Project configuration.
10+
grunt.initConfig({
11+
jshint: {
12+
options: {
13+
jshintrc: '.jshintrc',
14+
reporter: require('jshint-stylish')
15+
},
16+
dist: {
17+
src: ['dist/bootstrap-session-timeout.js']
18+
},
19+
test: {
20+
src: ['test/**/*.js']
21+
}
22+
},
23+
watch: {
24+
dist: {
25+
files: '<%= jshint.dist.src %>',
26+
tasks: ['jshint:dist']
27+
},
28+
test: {
29+
files: '<%= jshint.test.src %>',
30+
tasks: ['jshint:test']
31+
}
32+
},
33+
uglify: {
34+
dist: {
35+
files: {
36+
'dist/bootstrap-session-timeout.min.js': ['<%= jshint.dist.src %>']
37+
}
38+
}
39+
}
40+
});
41+
42+
// Default task.
43+
grunt.registerTask('default', ['jshint']);
44+
grunt.registerTask('dev', ['watch']);
45+
grunt.registerTask('min', ['jshint', 'uglify']);
46+
};

LICENSE.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MIT License (MIT)
2+
3+
Copyright (c) 2013 Travis Horn
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.
22+

README.md

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
# bootstrap-session-timeout
2+
Inspired by [jquery-sessionTimeout-bootstrap by maxfierke](https://github.com/maxfierke/jquery-sessionTimeout-bootstrap)
3+
4+
You can easily upgrade [jquery-sessionTimeout-bootstrap by maxfierke](https://github.com/maxfierke/jquery-sessionTimeout-bootstrap) to bootstrap-session-timeout. There have been a number of major upgrades. For example, as long as the user is doing something on the page, he will never get a timeout. The original plugin launched a timeout warning dialog in a fixed amount of time regardless of user activity. See description and documentation for more information.
5+
6+
## Description
7+
After a set amount of idle time, a Bootstrap warning dialog is shown to the user with the option to either log out, or stay connected. If "Logout" button is selected, the page is redirected to a logout URL. If "Stay Connected" is selected, a keep-alive URL is requested through AJAX (optional) to keep the session alive. If no option is selected after another set amount of idle time, the page is automatically redirected to a timeout URL.
8+
9+
Idle time is defined as no mouse, keyboard or touch event activity registered by the browser. As long as the user is active, the keep-alive URL also keeps getting pinged and the session stays alive. Optionally, you can also use this plugin as a simple lock mechanism on its own, if you have no need to keep the server-side session alive.
10+
11+
12+
## Getting Started
13+
14+
1. Include `jQuery`
15+
2. Include `bootstrap.js` and `bootstrap.css`<br>(to support the modal dialog, unless you plan on using your own callback)
16+
3. Include `bootstrap-session-timeout.js` or `bootstrap-session-timeout.min.js`<br>(from the /dist folder)
17+
4. Call `$.sessionTimeout();` after document ready
18+
19+
20+
21+
## Documentation
22+
### Options
23+
**message**<br>
24+
25+
Type: `String`
26+
27+
Default: `'Your session is about to expire.'`
28+
29+
Text shown to user in dialog after warning period.
30+
31+
**keepAliveUrl**
32+
33+
Type: `String`
34+
35+
Default: `'/keep-alive'`
36+
37+
URL to ping via AJAX POST to keep the session alive. This resource should do something innocuous that would keep the session alive, which will depend on your server-side platform.
38+
39+
**keepAlive**
40+
41+
Type: `Boolean`
42+
43+
Default: `true`
44+
45+
If `true`, pings the **keepAliveUrl** if the user clicks "Stay Connected" on the Bootstrap warning dialog. If you have no server-side session timeout to worry about, feel free to set this one to `false` to prevent unnecessary network activity.
46+
47+
**keepAliveInterval**
48+
49+
Type: `Integer`
50+
51+
Default: `5000` (5 seconds)
52+
53+
Minimum time in milliseconds between two keep-alive pings.
54+
55+
**redirUrl**
56+
57+
Type: `String`
58+
59+
Default: `'/timed-out'`
60+
61+
URL to take browser to if no action is take after warning period.
62+
63+
**logoutUrl**
64+
65+
Type: `String`
66+
67+
Default: `'/log-out'`
68+
69+
URL to take browser to if user clicks "Logout" on the Bootstrap warning dialog.
70+
71+
**warnAfter**
72+
73+
Type: `Integer`
74+
75+
Default: `900000` (15 minutes)
76+
77+
Time in milliseconds after page is opened until warning dialog is opened.
78+
79+
**redirAfter**
80+
81+
Type: `Integer`
82+
83+
Default: `1200000` (20 minutes)
84+
85+
Time in milliseconds after page is opened until browser is redirected to redirUrl.
86+
87+
**ignoreUserActivity**
88+
89+
Type: `Boolean`
90+
91+
Default: `false`
92+
93+
If `true`, this will launch the Bootstrap warning dialog / redirect (or callback functions) in a set amounts of time regardless of user activity.
94+
95+
**onWarn**
96+
97+
Type: `Function` or `Boolean`
98+
99+
Default: `false`
100+
101+
Custom callback you can use instead of showing the Bootstrap warning dialog. Note that if you need the keepAlive option, you will need to include it yourself in the callback function. See Examples below.
102+
103+
Redirect action will still occur unless you also use add the onRedir callback.
104+
105+
**onRedir**
106+
107+
Type: `Function` or `Boolean`
108+
109+
Default: false
110+
111+
Custom callback you can use instead of redirectiong the user to **redirUrl**.
112+
113+
## Examples
114+
115+
**Basic Usage**
116+
117+
Shows the warning dialog after one minute. The dialog is visible for another minute. If user takes no action, browser is redirected to `redirUrl`.
118+
119+
```js
120+
$.sessionTimeout({
121+
message: 'Your session will be locked in one minute.',
122+
keepAliveUrl: 'keep-alive.html',
123+
logoutUrl: 'login.html',
124+
redirUrl: 'locked.html',
125+
warnAfter: 60000,
126+
redirAfter: 120000
127+
});
128+
```
129+
130+
**With onWorn Callback**
131+
132+
Shows the "Warning!" alert after one minute. If user takes no action, after one more minute the browser is redirected to `redirUrl`.
133+
134+
```js
135+
$.sessionTimeout({
136+
redirUrl: 'locked.html',
137+
warnAfter: 60000,
138+
redirAfter: 120000,
139+
onWarn: function{
140+
alert('Warning!');
141+
}
142+
});
143+
```
144+
145+
**With both onWorn and onRedir Callback**
146+
147+
Console logs the "Your session will soon expire!" text after one minute. If user takes no action, after two more minutes the "Your session has expired!" alert gets shown. No redirection occurs.
148+
149+
```js
150+
$.sessionTimeout({
151+
warnAfter: 60000,
152+
redirAfter: 180000,
153+
onWarn: function{
154+
console.log('Your session will soon expire!');
155+
},
156+
onRedir: function{
157+
alert('Your session has expired!');
158+
}
159+
});
160+
```
161+
162+
## Contributing
163+
In lieu of a formal styleguide, take care to maintain the existing coding style. Add comments for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/).
164+
165+
## Release History
166+
* 2012-09-10   v1.0.0   Initial release.
167+
168+
## License
169+
Copyright (c) 2014 [Orange Hill](http://www.orangehilldev.com). Licensed under the MIT license.

dist/bootstrap-session-timeout.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* bootstrap-session-timeout
3+
* www.orangehilldev.com
4+
*
5+
* Copyright (c) 2014 Vedran Opacic
6+
* Licensed under the MIT license.
7+
*/
8+
9+
'use strict';
10+
11+
(function( $ ){
12+
jQuery.sessionTimeout = function( options ) {
13+
var defaults = {
14+
message : 'Your session is about to expire.',
15+
keepAliveUrl : '/keep-alive',
16+
redirUrl : '/timed-out',
17+
logoutUrl : '/log-out',
18+
warnAfter : 900000, // 15 minutes
19+
redirAfter : 1200000, // 20 minutes
20+
keepAliveInterval : 5000,
21+
keepAlive : true,
22+
ignoreUserActivity : false,
23+
onWarn : false,
24+
onRedir : false
25+
};
26+
27+
var opt = defaults,
28+
timer;
29+
30+
// extend user-set options over defaults
31+
if ( options ) {
32+
opt = $.extend( defaults, options );
33+
}
34+
35+
// some error handling if options are miss-configured
36+
if(opt.warnAfter >= opt.redirAfter){
37+
// for IE8 and earlier
38+
if (typeof console !== "undefined" || typeof console.error !== "undefined") {
39+
console.error('Bootstrap-session-timeout plugin is miss-configured. Option "redirAfter" must be equal or greater than "warnAfter".');
40+
}
41+
return false;
42+
}
43+
44+
// unless user set his own callback function, prepare bootstrap modal elements and events
45+
if(typeof opt.onWarn !== 'function'){
46+
// create timeout warning dialog
47+
$('body').append('<div class="modal fade" id="sessionTimeout-dialog"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button><h4 class="modal-title">Your Session is About to Expire!</h4></div><div class="modal-body">'+ opt.message +'</div><div class="modal-footer"><button id="sessionTimeout-dialog-logout" type="button" class="btn btn-default">Logout</button><button id="sessionTimeout-dialog-keepalive" type="button" class="btn btn-primary" data-dismiss="modal">Stay Connected</button></div></div></div></div>');
48+
49+
// "Logout" button click
50+
$('#sessionTimeout-dialog-logout').on('click', function () { window.location = opt.logoutUrl; });
51+
// "Stay Connected" button click
52+
$('#sessionTimeout-dialog').on('hide.bs.modal', function () {
53+
// restart session timer
54+
startSessionTimer();
55+
});
56+
}
57+
58+
// reset timer on any of these events
59+
if (!opt.ignoreUserActivity) {
60+
$(document).on('keyup mouseup mousemove touchend touchmove', function() {
61+
startSessionTimer();
62+
});
63+
}
64+
65+
// keeps the server side connection live, by pingin url set in keepAliveUrl option
66+
var keepAlivePinged = false;
67+
function keepAlive () {
68+
if (!keepAlivePinged){
69+
$.ajax({
70+
type: 'POST',
71+
url: opt.keepAliveUrl
72+
});
73+
keepAlivePinged = true;
74+
setTimeout(function() {
75+
keepAlivePinged = false;
76+
}, opt.keepAliveInterval);
77+
}
78+
}
79+
80+
function startSessionTimer(){
81+
// console.log('startSessionTimer()');
82+
// clear session timer
83+
clearTimeout(timer);
84+
85+
// if keepAlive option is set to "true", ping the "keepAliveUrl" url
86+
if (opt.keepAlive) {
87+
keepAlive();
88+
}
89+
90+
// set session timer
91+
timer = setTimeout(function(){
92+
// check for onWarn callback function and if there is none, launch dialog
93+
if(typeof opt.onWarn !== 'function'){
94+
$('#sessionTimeout-dialog').modal('show');
95+
}
96+
else {
97+
opt.onWarn('warn');
98+
}
99+
// start dialog timer
100+
startDialogTimer();
101+
}, opt.warnAfter);
102+
}
103+
104+
function startDialogTimer(){
105+
// console.log('startDialogTimer()');
106+
// clear session timer
107+
clearTimeout(timer);
108+
109+
// set dialog timer
110+
timer = setTimeout(function(){
111+
// check for onRedir callback function and if there is none, launch redirect
112+
if(typeof opt.onRedir !== 'function'){
113+
startDialogTimer('start');
114+
window.location = opt.redirUrl;
115+
}
116+
else {
117+
opt.onRedir();
118+
}
119+
}, (opt.redirAfter - opt.warnAfter));
120+
}
121+
122+
// start session timer
123+
startSessionTimer();
124+
};
125+
})( jQuery );

dist/bootstrap-session-timeout.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)