Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | espaco | 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 | // redirUrl |
||
| 33 | // URL to take browser to if no action is take after warning period |
||
| 34 | // Default: '/timed-out' |
||
| 35 | // |
||
| 36 | // logoutUrl |
||
| 37 | // URL to take browser to if user clicks "Log Out Now" |
||
| 38 | // Default: '/log-out' |
||
| 39 | // |
||
| 40 | // warnAfter |
||
| 41 | // Time in milliseconds after page is opened until warning dialog is opened |
||
| 42 | // Default: 900000 (15 minutes) |
||
| 43 | // |
||
| 44 | // redirAfter |
||
| 45 | // Time in milliseconds after page is opened until browser is redirected to redirUrl |
||
| 46 | // Default: 1200000 (20 minutes) |
||
| 47 | // |
||
| 48 | (function( $ ){ |
||
| 49 | jQuery.sessionTimeout = function( options ) { |
||
| 50 | var defaults = { |
||
| 51 | title : 'Session Notification', |
||
| 52 | message : 'Your session is about to expire.', |
||
| 53 | keepAliveUrl : '/keep-alive', |
||
| 54 | redirUrl : '/timed-out', |
||
| 55 | logoutUrl : '/log-out', |
||
| 56 | warnAfter : 900000, // 15 minutes |
||
| 57 | redirAfter : 1200000 // 20 minutes |
||
| 58 | }; |
||
| 59 | |||
| 60 | // Extend user-set options over defaults |
||
| 61 | var o = defaults, |
||
| 62 | dialogTimer, |
||
| 63 | redirTimer; |
||
| 64 | |||
| 65 | if ( options ) { o = $.extend( defaults, options ); } |
||
| 66 | |||
| 67 | // Create timeout warning dialog |
||
| 68 | $('body').append('<div class="modal fade" id="sessionTimeout-dialog"><div class="modal-dialog modal-small"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button><h4 class="modal-title">'+ o.title +'</h4></div><div class="modal-body">'+ o.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>'); |
||
| 69 | $('#sessionTimeout-dialog-logout').on('click', function () { window.location = o.logoutUrl; }); |
||
| 70 | $('#sessionTimeout-dialog').on('hide.bs.modal', function () { |
||
| 71 | $.ajax({ |
||
| 72 | type: 'POST', |
||
| 73 | url: o.keepAliveUrl |
||
| 74 | }); |
||
| 75 | |||
| 76 | // Stop redirect timer and restart warning timer |
||
| 77 | controlRedirTimer('stop'); |
||
| 78 | controlDialogTimer('start'); |
||
| 79 | }) |
||
| 80 | |||
| 81 | function controlDialogTimer(action){ |
||
| 82 | switch(action) { |
||
| 83 | case 'start': |
||
| 84 | // After warning period, show dialog and start redirect timer |
||
| 85 | dialogTimer = setTimeout(function(){ |
||
| 86 | $('#sessionTimeout-dialog').modal('show'); |
||
| 87 | controlRedirTimer('start'); |
||
| 88 | }, o.warnAfter); |
||
| 89 | break; |
||
| 90 | |||
| 91 | case 'stop': |
||
| 92 | clearTimeout(dialogTimer); |
||
| 93 | break; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | function controlRedirTimer(action){ |
||
| 98 | switch(action) { |
||
| 99 | case 'start': |
||
| 100 | // Dialog has been shown, if no action taken during redir period, redirect |
||
| 101 | redirTimer = setTimeout(function(){ |
||
| 102 | window.location = o.redirUrl; |
||
| 103 | }, o.redirAfter - o.warnAfter); |
||
| 104 | break; |
||
| 105 | |||
| 106 | case 'stop': |
||
| 107 | clearTimeout(redirTimer); |
||
| 108 | break; |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | // Begin warning period |
||
| 113 | controlDialogTimer('start'); |
||
| 114 | }; |
||
| 115 | })( jQuery ); |