Cross-tab Session Timeout Warning Plugin For - jQuery jTimeout

Cross-tab Session Timeout Warning Plugin For - jQuery jTimeout
File Size: 21.6 KB
Views Total:
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   

jTimeout is a small yet highly configurable session timeout and keep-alive control implemented in jQuery.

The plugin displays a warning modal dialog to the user when the session is about to expire, with the options to either logout, or extend the session.

After the session is expired, the plugin displays a modal dialog that redirects the user to the login url you specify.

More features:

  • Cross browser tabs.
  • Flashes the tab/title bar when the session is about to expire.
  • Automatically extends the session when the mouse moves.
  • Callback functions.

How to use it:

1. Download and load the minified version of the jQuery jTimeout plugin after jQuery library.

<script src="https://code.jquery.com/jquery-3.3.1.min.js" 
        integrity="sha384-tsQFqpEReu7ZLhBV2VZlAu7zcOV+rXbYlF2cqB8txI/8aZajjp4Bqd+V6D5IgvKT" 
        crossorigin="anonymous">
</script>
<script src="dist/jTimeout.min.js"></script>

2. By default, the plugin uses the jQuery jAlert plugin to provide the warning dialog. 

<link href="jAlert.min.css" rel="stylesheet">
<script src="jAlert.min.js"></script>

3. Activate the session timeout plugin and specify the timeout in seconds and login/logout/extend URLs.

$.jTimeout({
  'timeoutAfter': 60 // default: 1440
  'loginUrl': 'login.html',
  'logoutUrl': 'logout.html',
  'extendUrl': 'index.html'
});

4. All default configuration options.

$.jTimeout({

  // flash the tab/title bar when or after timing out
  flashTitle: true,

  // flash speed in milliseconds
  flashTitleSpeed: 500, 

  // text to show in the tab/title bar
  flashingTitleText: '**WARNING**',

  // original page title
  originalTitle: document.title,

  // timeout in seconds
  timeoutAfter: 1440,

  // seconds in between checking and updating the timer
  heartbeat: 1, 

  // extends the session when the mouse is moved
  extendOnMouseMove: true,

  // seconds between extending the session when the mouse is moved
  mouseDebounce: 30,

  // on mouse move function
  onMouseMove: function(timeout){
      //request the session extend page
      $.get(timeout.options.extendUrl, function(){
          //reset expiration
          timeout.resetExpiration();
      });
  }, 

  // custom URL
  extendUrl: '/dashboard',
  logoutUrl: '/logout',
  loginUrl: '/login',

  // seconds before timeout to fire the next callback
  secondsPrior: 60, 

  // resets the timer with mouse move while the dialog is visible and hide it
  triggerResetOnAlert: false
  
});

5. Execute a function when clicking the extend button.

$.jTimeout({

  onClickExtend: function(timeout){
    /* Request dashboard to increase session */
    $.get(timeout.options.extendUrl);
    timeout.resetExpiration();
  }

});

6. Execute a function after timeout.

$.jTimeout({

  onTimeout: function(timeout){
    /* Alert User */
    $.jAlert({
        'id': 'jTimedoutAlert',
        'title': 'Oh No!',
        'content': '<b>Your session has timed out.</b>',
        'theme': 'red',
        'btns': {
            'text': 'Login Again',
            'href': timeout.options.loginUrl,
            'theme': 'blue',
            'closeAlert': false
        },
        'closeOnClick': false,
        'closeBtn': false,
        'closeOnEsc': false
    });

    /* Force logout */
    $.get(timeout.options.logoutUrl);
  },

});

7. Execute a function when the session is extended.

$.jTimeout({

  onSessionExtended: function(timeout) {
    $('#jTimedoutAlert').closeAlert();
  }

});

8. If you want to uses another dialog plugin to replace the jAlert plugin.

$.jTimeout({

  onPriorCallback: function(timeout, seconds){
    $.jAlert({
        'id': 'jTimeoutAlert',
        'title': 'Oh No!',
        'content': '<b>Your session will timeout in <span class="jTimeout_Countdown">' + seconds + '</span> seconds!</b>',
        'theme': 'red',
        'closeBtn': false,
        'onOpen': function (alert) {
            timeout.startPriorCountdown(alert.find('.jTimeout_Countdown'));
        },
        'btns': [
            {
                'text': 'Extend my Session',
                'theme': 'blue',
                'onClick': function (e, btn) {
                    e.preventDefault();
                    timeout.options.onClickExtend(timeout);
                    btn.parents('.jAlert').closeAlert();
                    return false;
                }
            },
            {
                'text': 'Logout Now',
                'onClick': function (e, btn) {
                    e.preventDefault();
                    window.location.href = timeout.options.logoutUrl;
                    return false;
                }
            }
        ]
    });
  },

});

9. API methods available.

// reset
$.jTimeout().reset();

// set expiration
$.jTimeout().setExpiration(1000); 

// get expiration
$.jTimeout().getExpiration();

// get the number of seconds until the session expires
$.jTimeout().getSecondsTillExpiration(); 

This awesome jQuery plugin is developed by HTMLGuyLLC. For more Advanced Usages, please check the demo page or visit the official website.