├── .gitignore ├── .travis.yml ├── gruntfile.js ├── bower.json ├── package.json ├── LICENSE.md ├── jquery.sessionTimeout.min.js ├── README.md └── jquery.sessionTimeout.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | before_script: 5 | - npm install grunt-cli -g -------------------------------------------------------------------------------- /gruntfile.js: -------------------------------------------------------------------------------- 1 | module.exports = function(grunt) { 2 | grunt.initConfig({ 3 | jshint: { 4 | build: { 5 | files: { 6 | src: ['jquery.sessionTimeout.js'] 7 | } 8 | } 9 | }, 10 | uglify: { 11 | build: { 12 | src : 'jquery.sessionTimeout.js', 13 | dest : 'jquery.sessionTimeout.min.js' 14 | } 15 | } 16 | }); 17 | 18 | grunt.loadNpmTasks('grunt-contrib-jshint'); 19 | grunt.loadNpmTasks('grunt-contrib-uglify'); 20 | 21 | grunt.registerTask('default', ['jshint', 'uglify']); 22 | grunt.registerTask('test', ['jshint']); 23 | }; -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jquery.sessionTimeout", 3 | "version": "1.1.0", 4 | "main": "jquery.sessionTimeout.js", 5 | "homepage": "https://github.com/travishorn/jquery-sessionTimeout", 6 | "authors": [ 7 | "Travis Horn" 8 | ], 9 | "description": "After a set amount of time, a dialog is shown to the user with the option to either log out now, or stay connected. If log out now is selected, the page is redirected to a logout URL. If stay connected is selected, a keep-alive URL is requested through AJAX. If no options is selected after another set amount of time, the page is automatically redirected to a timeout URL.", 10 | "keywords": [ 11 | "jquery", 12 | "session timeout" 13 | ], 14 | "bugs": "https://github.com/travishorn/jquery-sessionTimeout/issues", 15 | "license": "MIT" 16 | } 17 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jquery-sessiontimeout", 3 | "version": "1.1.0", 4 | "description": "After a set amount of time, a dialog is shown to the user with the option to either log out now, or stay connected. If log out now is selected, the page is redirected to a logout URL. If stay connected is selected, a keep-alive URL is requested through AJAX. If no options is selected after another set amount of time, the page is automatically redirected to a timeout URL.", 5 | "main": "jquery.sessionTimeout.js", 6 | "scripts": { 7 | "test": "grunt test" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/travishorn/jquery-sessionTimeout.git" 12 | }, 13 | "keywords": [ 14 | "jquery-plugin", 15 | "ecosystem:jquery", 16 | "alert", 17 | "ajax" 18 | ], 19 | "author": "Travis Horn", 20 | "license": "MIT", 21 | "bugs": "https://github.com/travishorn/jquery-sessionTimeout/issues", 22 | "devDependencies": { 23 | "grunt": "~0.4.1", 24 | "grunt-contrib-jshint": "~0.6.0", 25 | "grunt-contrib-uglify": "~0.2.2" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 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 | 23 | -------------------------------------------------------------------------------- /jquery.sessionTimeout.min.js: -------------------------------------------------------------------------------- 1 | !function(a){jQuery.sessionTimeout=function(b){function c(b){switch(b){case"start":f=setTimeout(function(){a("#sessionTimeout-dialog").dialog("open"),d("start")},i.warnAfter);break;case"stop":clearTimeout(f)}}function d(a){switch(a){case"start":g=setTimeout(function(){window.location=i.redirUrl},i.redirAfter-i.warnAfter);break;case"stop":clearTimeout(g)}}function e(a,b,c){var d=new RegExp("([?|&])"+b+"=.*?(&|#|$)","i");if(a.match(d))return a.replace(d,"$1"+b+"="+c+"$2");var e="";-1!==a.indexOf("#")&&(e=a.replace(/.*#/,"#"),a=a.replace(/#.*/,""));var f=-1!==a.indexOf("?")?"&":"?";return a+f+b+"="+c+e}var f,g,h={message:"Your session is about to expire.",keepAliveUrl:"/keep-alive",keepAliveAjaxRequestType:"POST",redirUrl:"/timed-out",logoutUrl:"/log-out",warnAfter:9e5,redirAfter:12e5,appendTime:!0},i=h;b&&(i=a.extend(h,b)),a("body").append('
'+i.message+"
"),a("#sessionTimeout-dialog").dialog({autoOpen:!1,width:400,modal:!0,closeOnEscape:!1,open:function(){a(".ui-dialog-titlebar-close").hide()},buttons:{"Log Out Now":function(){window.location=i.logoutUrl},"Stay Connected":function(){a(this).dialog("close"),a.ajax({type:i.keepAliveAjaxRequestType,url:i.appendTime?e(i.keepAliveUrl,"_",(new Date).getTime()):i.keepAliveUrl}),d("stop"),c("start")}}}),a(document).ajaxComplete(function(){a("#sessionTimeout-dialog").dialog("isOpen")||(d("stop"),c("stop"),c("start"))}),c("start")}}(jQuery); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sessionTimeout 2 | [![Build Status](https://api.travis-ci.org/travishorn/jquery-sessionTimeout.png)](https://travis-ci.org/travishorn/jquery-sessionTimeout) 3 | 4 | --- 5 | 6 | ## ⚠️ NOTICE 7 | 8 | This is an older version. The new version is a complete re-write with no dependencies. See 9 | [the new version here](https://github.com/travishorn/session-timeout). 10 | 11 | --- 12 | 13 | ## Description 14 | After a set amount of time, a dialog is shown to the user with the option to either log out now, or stay connected. If log out now is selected, the page is redirected to a logout URL. If stay connected is selected, a keep-alive URL is requested through AJAX. If no options is selected after another set amount of time, the page is automatically redirected to a timeout URL. 15 | 16 | ## Usage 17 | 1. Include jQuery 18 | 2. Include jQuery UI (for dialog) 19 | 3. Include jquery.sessionTimeout.js 20 | 4. Call `$.sessionTimeout();` after document ready 21 | 22 | ## Options 23 | **message**
24 | Text shown to user in dialog after warning period. 25 | Default: 'Your session is about to expire.' 26 | 27 | **keepAliveUrl**
28 | URL to call through AJAX to keep session alive. This resource should do something innocuous that would keep the session alive, which will depend on your server-side platform.
29 | Default: '/keep-alive' 30 | 31 | **keepAliveAjaxRequestType**
32 | How should we make the call to the keep-alive url? (GET/POST/PUT)
33 | Default: 'POST' 34 | 35 | **redirUrl**
36 | URL to take browser to if no action is take after warning period.
37 | Default: '/timed-out' 38 | 39 | **logoutUrl**
40 | URL to take browser to if user clicks "Log Out Now".
41 | Default: '/log-out' 42 | 43 | **warnAfter**
44 | Time in milliseconds after page is opened until warning dialog is opened.
45 | Default: 900000 (15 minutes) 46 | 47 | **redirAfter**
48 | Time in milliseconds after page is opened until browser is redirected to redirUrl.
49 | Default: 1200000 (20 minutes) 50 | 51 | **appendTime**
52 | If true, appends the current time stamp to the Keep Alive url to prevent caching issues
53 | Default: true 54 | 55 | ## Links 56 | * [Online Demo](http://codepen.io/anon/pen/qEzNpd) 57 | * [Screenshots](https://sites.google.com/site/tpopsjqueryplugins/sessiontimeout/screenshots) 58 | -------------------------------------------------------------------------------- /jquery.sessionTimeout.js: -------------------------------------------------------------------------------- 1 | /*jshint browser:true*/ 2 | 3 | // 4 | // jquery.sessionTimeout.js 5 | // 6 | // After a set amount of time, a dialog is shown to the user with the option 7 | // to either log out now, or stay connected. If log out now is selected, 8 | // the page is redirected to a logout URL. If stay connected is selected, 9 | // a keep-alive URL is requested through AJAX. If no options is selected 10 | // after another set amount of time, the page is automatically redirected 11 | // to a timeout URL. 12 | // 13 | // 14 | // USAGE 15 | // 16 | // 1. Include jQuery 17 | // 2. Include jQuery UI (for dialog) 18 | // 3. Include jquery.sessionTimeout.js 19 | // 4. Call $.sessionTimeout(); after document ready 20 | // 21 | // 22 | // OPTIONS 23 | // 24 | // message 25 | // Text shown to user in dialog after warning period. 26 | // Default: 'Your session is about to expire.' 27 | // 28 | // keepAliveUrl 29 | // URL to call through AJAX to keep session alive. This resource should do something innocuous that would keep the session alive, which will depend on your server-side platform. 30 | // Default: '/keep-alive' 31 | // 32 | // keepAliveAjaxRequestType 33 | // How should we make the call to the keep-alive url? (GET/POST/PUT) 34 | // Default: 'POST' 35 | // 36 | // redirUrl 37 | // URL to take browser to if no action is take after warning period 38 | // Default: '/timed-out' 39 | // 40 | // logoutUrl 41 | // URL to take browser to if user clicks "Log Out Now" 42 | // Default: '/log-out' 43 | // 44 | // warnAfter 45 | // Time in milliseconds after page is opened until warning dialog is opened 46 | // Default: 900000 (15 minutes) 47 | // 48 | // redirAfter 49 | // Time in milliseconds after page is opened until browser is redirected to redirUrl 50 | // Default: 1200000 (20 minutes) 51 | // 52 | // appendTime 53 | // If true, appends the current time stamp to the Keep Alive url to prevent caching issues 54 | // Default: true 55 | // 56 | (function ($) { 57 | jQuery.sessionTimeout = function (options) { 58 | var defaults = { 59 | message: 'Your session is about to expire.', 60 | keepAliveUrl: '/keep-alive', 61 | keepAliveAjaxRequestType: 'POST', 62 | redirUrl: '/timed-out', 63 | logoutUrl: '/log-out', 64 | warnAfter: 900000, // 15 minutes 65 | redirAfter: 1200000, // 20 minutes 66 | appendTime: true // appends time stamp to keep alive url to prevent caching 67 | }; 68 | 69 | // Extend user-set options over defaults 70 | var o = defaults, 71 | dialogTimer, 72 | redirTimer; 73 | 74 | if (options) { o = $.extend(defaults, options); } 75 | 76 | // Create timeout warning dialog 77 | $('body').append('
' + o.message + '
'); 78 | $('#sessionTimeout-dialog').dialog({ 79 | autoOpen: false, 80 | width: 400, 81 | modal: true, 82 | closeOnEscape: false, 83 | open: function () { $(".ui-dialog-titlebar-close").hide(); }, 84 | buttons: { 85 | // Button one - takes user to logout URL 86 | "Log Out Now": function () { 87 | window.location = o.logoutUrl; 88 | }, 89 | // Button two - closes dialog and makes call to keep-alive URL 90 | "Stay Connected": function () { 91 | $(this).dialog('close'); 92 | 93 | $.ajax({ 94 | type: o.keepAliveAjaxRequestType, 95 | url: o.appendTime ? updateQueryStringParameter(o.keepAliveUrl, "_", new Date().getTime()) : o.keepAliveUrl 96 | }); 97 | 98 | // Stop redirect timer and restart warning timer 99 | controlRedirTimer('stop'); 100 | controlDialogTimer('start'); 101 | } 102 | } 103 | }); 104 | 105 | function controlDialogTimer(action) { 106 | switch (action) { 107 | case 'start': 108 | // After warning period, show dialog and start redirect timer 109 | dialogTimer = setTimeout(function () { 110 | $('#sessionTimeout-dialog').dialog('open'); 111 | controlRedirTimer('start'); 112 | }, o.warnAfter); 113 | break; 114 | 115 | case 'stop': 116 | clearTimeout(dialogTimer); 117 | break; 118 | } 119 | } 120 | 121 | function controlRedirTimer(action) { 122 | switch (action) { 123 | case 'start': 124 | // Dialog has been shown, if no action taken during redir period, redirect 125 | redirTimer = setTimeout(function () { 126 | window.location = o.redirUrl; 127 | }, o.redirAfter - o.warnAfter); 128 | break; 129 | 130 | case 'stop': 131 | clearTimeout(redirTimer); 132 | break; 133 | } 134 | } 135 | 136 | // Courtesy of http://stackoverflow.com/questions/5999118/add-or-update-query-string-parameter 137 | // Includes fix for angular ui-router as per comment by j_walker_dev 138 | function updateQueryStringParameter(uri, key, value) { 139 | var re = new RegExp("([?|&])" + key + "=.*?(&|#|$)", "i"); 140 | 141 | if (uri.match(re)) { 142 | return uri.replace(re, '$1' + key + "=" + value + '$2'); 143 | } else { 144 | var hash = ''; 145 | 146 | if (uri.indexOf('#') !== -1) { 147 | hash = uri.replace(/.*#/, '#'); 148 | uri = uri.replace(/#.*/, ''); 149 | } 150 | 151 | var separator = uri.indexOf('?') !== -1 ? "&" : "?"; 152 | return uri + separator + key + "=" + value + hash; 153 | } 154 | } 155 | 156 | $(document).ajaxComplete(function () { 157 | if (!$('#sessionTimeout-dialog').dialog("isOpen")) { 158 | controlRedirTimer('stop'); 159 | controlDialogTimer('stop'); 160 | controlDialogTimer('start'); 161 | } 162 | }); 163 | 164 | // Begin warning period 165 | controlDialogTimer('start'); 166 | }; 167 | })(jQuery); 168 | --------------------------------------------------------------------------------