jquery.sessionTimeout.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*jshint browser:true*/
  2. //
  3. // jquery.sessionTimeout.js
  4. //
  5. // After a set amount of time, a dialog is shown to the user with the option
  6. // to either log out now, or stay connected. If log out now is selected,
  7. // the page is redirected to a logout URL. If stay connected is selected,
  8. // a keep-alive URL is requested through AJAX. If no options is selected
  9. // after another set amount of time, the page is automatically redirected
  10. // to a timeout URL.
  11. //
  12. //
  13. // USAGE
  14. //
  15. // 1. Include jQuery
  16. // 2. Include jQuery UI (for dialog)
  17. // 3. Include jquery.sessionTimeout.js
  18. // 4. Call $.sessionTimeout(); after document ready
  19. //
  20. //
  21. // OPTIONS
  22. //
  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. // redirUrl
  32. // URL to take browser to if no action is take after warning period
  33. // Default: '/timed-out'
  34. //
  35. // logoutUrl
  36. // URL to take browser to if user clicks "Log Out Now"
  37. // Default: '/log-out'
  38. //
  39. // warnAfter
  40. // Time in milliseconds after page is opened until warning dialog is opened
  41. // Default: 900000 (15 minutes)
  42. //
  43. // redirAfter
  44. // Time in milliseconds after page is opened until browser is redirected to redirUrl
  45. // Default: 1200000 (20 minutes)
  46. //
  47. (function( $ ){
  48. jQuery.sessionTimeout = function( options ) {
  49. var defaults = {
  50. title : 'Session Notification',
  51. message : 'Your session is about to expire.',
  52. keepAliveUrl : '/keep-alive',
  53. redirUrl : '/timed-out',
  54. logoutUrl : '/log-out',
  55. warnAfter : 900000, // 15 minutes
  56. redirAfter : 1200000 // 20 minutes
  57. };
  58. // Extend user-set options over defaults
  59. var o = defaults,
  60. dialogTimer,
  61. redirTimer;
  62. if ( options ) { o = $.extend( defaults, options ); }
  63. // Create timeout warning dialog
  64. $('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">&times;</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>');
  65. $('#sessionTimeout-dialog-logout').on('click', function () { window.location = o.logoutUrl; });
  66. $('#sessionTimeout-dialog').on('hide.bs.modal', function () {
  67. $.ajax({
  68. type: 'POST',
  69. url: o.keepAliveUrl
  70. });
  71. // Stop redirect timer and restart warning timer
  72. controlRedirTimer('stop');
  73. controlDialogTimer('start');
  74. })
  75. function controlDialogTimer(action){
  76. switch(action) {
  77. case 'start':
  78. // After warning period, show dialog and start redirect timer
  79. dialogTimer = setTimeout(function(){
  80. $('#sessionTimeout-dialog').modal('show');
  81. controlRedirTimer('start');
  82. }, o.warnAfter);
  83. break;
  84. case 'stop':
  85. clearTimeout(dialogTimer);
  86. break;
  87. }
  88. }
  89. function controlRedirTimer(action){
  90. switch(action) {
  91. case 'start':
  92. // Dialog has been shown, if no action taken during redir period, redirect
  93. redirTimer = setTimeout(function(){
  94. window.location = o.redirUrl;
  95. }, o.redirAfter - o.warnAfter);
  96. break;
  97. case 'stop':
  98. clearTimeout(redirTimer);
  99. break;
  100. }
  101. }
  102. // Begin warning period
  103. controlDialogTimer('start');
  104. };
  105. })( jQuery );