autologout.html 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset='utf-8' />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <meta name="description" content="Jquery-idletimer : provides you a way to monitor user activity with a page." />
  8. <title>Jquery-idletimer</title>
  9. <!-- jQuery and idleTimer -->
  10. <script type="text/javascript" src="//code.jquery.com/jquery-1.11.0.min.js"></script>
  11. <script type="text/javascript" src="../src/idle-timer.js"></script>
  12. <!-- Bootstrap/respond (ie8) and moment -->
  13. <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
  14. <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
  15. <script src="//cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js"></script>
  16. <script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.5.1/moment.js"></script>
  17. <!-- Respond.js proxy on external server -->
  18. <link href="//netdna.bootstrapcdn.com/respond-proxy.html" id="respond-proxy" rel="respond-proxy" />
  19. <link href="respond.proxy.gif" id="respond-redirect" rel="respond-redirect" />
  20. <script src="respond.proxy.js"></script>
  21. <style>
  22. .bold {
  23. font-weight: bold;
  24. }
  25. body {
  26. margin-top: 40px;
  27. margin-bottom: 40px;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <div class="navbar navbar-default navbar-fixed-top" role="navigation">
  33. <div class="container">
  34. <div class="navbar-header">
  35. <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
  36. <span class="sr-only">Toggle navigation</span>
  37. <span class="icon-bar"></span>
  38. <span class="icon-bar"></span>
  39. <span class="icon-bar"></span>
  40. </button>
  41. <a class="navbar-brand" href="index.html">Jquery-idletimer</a>
  42. </div>
  43. <div class="navbar-collapse collapse">
  44. <ul class="nav navbar-nav">
  45. <li><a href="index.html">Main Demo</a></li>
  46. <li><a href="defaultbinding.html">Default Binding</a></li>
  47. <li><a href="https://github.com/thorst/jquery-idletimer">Documentation</a></li>
  48. </ul>
  49. <ul class="nav navbar-nav navbar-right">
  50. <li><a href="https://github.com/thorst/jquery-idletimer/zipball/master">Zip</a></li>
  51. <li><a href="https://github.com/thorst/jquery-idletimer/tarball/master">Tar</a></li>
  52. <li><a href="https://github.com/thorst/jquery-idletimer">Github</a></li>
  53. </ul>
  54. </div>
  55. <!--/.nav-collapse -->
  56. </div>
  57. </div>
  58. <div class="container">
  59. <h2>Concept</h2>
  60. <p>
  61. Wait 10 seconds, you will see a expiring warning. Wait 10 more seconds and you will see that you have been logged out.
  62. </p>
  63. <p>
  64. In the real world I forward them to the logout url, which intern fowards them to login screen, instead of showing the 2nd dialog.
  65. You can modify the session.logout function.
  66. </p>
  67. <p>
  68. We could use the active.idleTimer event to clearTimeout, however I prefer the user to explicitly say they want to keep the
  69. session open by clicking ok, not just moving the mouse on the screen.
  70. </p>
  71. <p>
  72. This demo takes into account when a mobile device closes the browser, and after the idle timeout expires, launches the browser again. Instead
  73. of displaying the warning, it will jump straight to the logged out dialog.
  74. </p>
  75. <p>
  76. For the sake of complete demo, I've included the code needed to call a keepalive url to keep the server side session valid.
  77. </p>
  78. </div>
  79. <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
  80. <div class="modal-dialog">
  81. <div class="modal-content">
  82. <div class="modal-header">
  83. <h4 class="modal-title">Session Expiration Warning</h4>
  84. </div>
  85. <div class="modal-body">
  86. <p>You've been inactive for a while. For your security, we'll log you out automatically. Click "Stay Online" to continue your session. </p>
  87. <p>Your session will expire in <span class="bold" id="sessionSecondsRemaining">120</span> seconds.</p>
  88. </div>
  89. <div class="modal-footer">
  90. <button id="extendSession" type="button" class="btn btn-default btn-success" data-dismiss="modal">Stay Online</button>
  91. <button id="logoutSession" type="button" class="btn btn-default" data-dismiss="modal">Logout</button>
  92. </div>
  93. </div>
  94. </div>
  95. </div>
  96. <div class="modal fade" id="mdlLoggedOut" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
  97. <div class="modal-dialog">
  98. <div class="modal-content">
  99. <div class="modal-header">
  100. <h4 class="modal-title">You have been logged out</h4>
  101. </div>
  102. <div class="modal-body">
  103. <p>Your session has expired.</p>
  104. </div>
  105. <div class="modal-footer">
  106. </div>
  107. </div>
  108. </div>
  109. </div>
  110. <script>
  111. (function ($) {
  112. var
  113. session = {
  114. //Logout Settings
  115. inactiveTimeout: 10000, //(ms) The time until we display a warning message
  116. warningTimeout: 10000, //(ms) The time until we log them out
  117. minWarning: 5000, //(ms) If they come back to page (on mobile), The minumum amount, before we just log them out
  118. warningStart: null, //Date time the warning was started
  119. warningTimer: null, //Timer running every second to countdown to logout
  120. logout: function () { //Logout function once warningTimeout has expired
  121. //window.location = settings.autologout.logouturl;
  122. $("#mdlLoggedOut").modal("show");
  123. },
  124. //Keepalive Settings
  125. keepaliveTimer: null,
  126. keepaliveUrl: "",
  127. keepaliveInterval: 5000, //(ms) the interval to call said url
  128. keepAlive: function () {
  129. $.ajax({ url: session.keepaliveUrl });
  130. }
  131. }
  132. ;
  133. $(document).on("idle.idleTimer", function (event, elem, obj) {
  134. //Get time when user was last active
  135. var
  136. diff = (+new Date()) - obj.lastActive - obj.timeout,
  137. warning = (+new Date()) - diff
  138. ;
  139. //On mobile js is paused, so see if this was triggered while we were sleeping
  140. if (diff >= session.warningTimeout || warning <= session.minWarning) {
  141. $("#mdlLoggedOut").modal("show");
  142. } else {
  143. //Show dialog, and note the time
  144. $('#sessionSecondsRemaining').html(Math.round((session.warningTimeout - diff) / 1000));
  145. $("#myModal").modal("show");
  146. session.warningStart = (+new Date()) - diff;
  147. //Update counter downer every second
  148. session.warningTimer = setInterval(function () {
  149. var remaining = Math.round((session.warningTimeout / 1000) - (((+new Date()) - session.warningStart) / 1000));
  150. if (remaining >= 0) {
  151. $('#sessionSecondsRemaining').html(remaining);
  152. } else {
  153. session.logout();
  154. }
  155. }, 1000)
  156. }
  157. });
  158. // create a timer to keep server session alive, independent of idle timer
  159. session.keepaliveTimer = setInterval(function () {
  160. session.keepAlive();
  161. }, session.keepaliveInterval);
  162. //User clicked ok to extend session
  163. $("#extendSession").click(function () {
  164. clearTimeout(session.warningTimer);
  165. });
  166. //User clicked logout
  167. $("#logoutSession").click(function () {
  168. session.logout();
  169. });
  170. //Set up the timer, if inactive for 10 seconds log them out
  171. $(document).idleTimer(session.inactiveTimeout);
  172. })(jQuery);
  173. </script>
  174. </body>
  175. </html>