Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session lost when there is a pending AJAX request #43

Open
feldenla opened this issue Mar 1, 2017 · 7 comments
Open

Session lost when there is a pending AJAX request #43

feldenla opened this issue Mar 1, 2017 · 7 comments
Assignees

Comments

@feldenla
Copy link

feldenla commented Mar 1, 2017

Thanks for the plugin.

However, I have a long running script that takes longer than the idleTimeLimit that I set and hence the plugin logs out the user when the idleTimeLimit is reached.

I would therefore like to know if the plugin can detect an active AJAX request and wait for it to complete before the idle timer begins, that is, can active AJAX request be detected as an active event.

@JillElaine JillElaine self-assigned this Mar 1, 2017
@JillElaine
Copy link
Owner

You will need to modify the code to check if there is an active AJAX request. Load the jquery-idleTimeout-for-testing.js script, https://github.com/JillElaine/jquery-idleTimeout/blob/master/jquery-idleTimeout-for-testing.js, on your page and watch the console (eg: use Firefox Developer Tools https://developer.mozilla.org/en-US/docs/Tools/Web_Console/Opening_the_Web_Console ) to see the output. This is very useful for debugging & understanding how the script works.

Next, modify the checkIdleTimeout function to include a test for active AJAX connections. The checkIdleTimeout function polls every idleCheckHeartbeat seconds. It opens the warning dialog when certain conditions are met. Set a 'condition' that there must be no active connections before the dialog opens. See untested, modified code below.

You'll probably want to 'comment out' many of the extra 'console.log' outputs to focus only on the outputs you care about.

 //----------- IDLE TIMER FUNCTIONS --------------//
    checkIdleTimeout = function () {

if ($.active > 0) { // start of $.active check
  console.log('ACTIVE connection! NO TIMEOUT YET!');
} else {
  console.log('NO active connections. Proceed to check other timeout conditions.');

      var timeIdleTimeout = (store.get('idleTimerLastActivity') + (currentConfig.idleTimeLimit * 1000));

      if ($.now() > timeIdleTimeout) {
        console.log('inactivity has exceeded the idleTimeLimit');

        if (!currentConfig.enableDialog) { // warning dialog is disabled
          console.log('warning dialog disabled - log out user without warning');
          logoutUser(); // immediately log out user when user is idle for idleTimeLimit
        } else if (currentConfig.enableDialog && isDialogOpen() !== true) {
          console.log('warning dialog is not open & will be opened');
          openWarningDialog();
          startDialogTimer(); // start timing the warning dialog
        }
      } else if (store.get('idleTimerLoggedOut') === true) { //a 'manual' user logout?
        console.log('user has manually logged out? Log out all windows & tabs now.');
        logoutUser();
      } else {
        console.log('inactivity has not yet exceeded the idleTimeLimit');

        if (currentConfig.enableDialog && isDialogOpen() === true) {
          console.log('warning dialog is open & will be closed');
          destroyWarningDialog();
          stopDialogTimer();
        }
      }
} // end of $.active check
};

Please report back. Thank you.

@feldenla
Copy link
Author

feldenla commented Mar 1, 2017

Great work and thanks a lot for your timely reply.

It is working somehow as expected, that is, it keeps on waiting while there is an active AJAX connection.
So the user is active until the AJAX request is complete, however the user is logged out immediately the AJAX connection is closed and logs the message 'inactivity has exceeded the idleTimeLimit'.

It is obvious here that;

if ($.now() > timeIdleTimeout) { // would always be true }

Can we therefore reset store.get('idleTimerLastActivity') to the current time if $.active > 0 and also stop pinging the server again to keep the session alive since there is an already active connection.

Your help would be greatly appreciated.
Thank you.

@JillElaine
Copy link
Owner

Ah, yes, in the checkIdleTimeout function, you could reset the idleTimerLastActivity storage variable to the current time if there are active connections. Does that work as desired?

 //----------- IDLE TIMER FUNCTIONS --------------//
    checkIdleTimeout = function () {

if ($.active > 0) { // start of $.active check
  console.log('ACTIVE connection! NO TIMEOUT YET!');
  // reset 'idleTimerLastActivity' storage variable to current time
  store.set('idleTimerLastActivity', $.now());
} else {
  console.log('NO active connections. Proceed to check other timeout conditions.');

      var timeIdleTimeout = (store.get('idleTimerLastActivity') + (currentConfig.idleTimeLimit * 1000));

      if ($.now() > timeIdleTimeout) {
        console.log('inactivity has exceeded the idleTimeLimit');

        if (!currentConfig.enableDialog) { // warning dialog is disabled
          console.log('warning dialog disabled - log out user without warning');
          logoutUser(); // immediately log out user when user is idle for idleTimeLimit
        } else if (currentConfig.enableDialog && isDialogOpen() !== true) {
          console.log('warning dialog is not open & will be opened');
          openWarningDialog();
          startDialogTimer(); // start timing the warning dialog
        }
      } else if (store.get('idleTimerLoggedOut') === true) { //a 'manual' user logout?
        console.log('user has manually logged out? Log out all windows & tabs now.');
        logoutUser();
      } else {
        console.log('inactivity has not yet exceeded the idleTimeLimit');

        if (currentConfig.enableDialog && isDialogOpen() === true) {
          console.log('warning dialog is open & will be closed');
          destroyWarningDialog();
          stopDialogTimer();
        }
      }
} // end of $.active check
};

@feldenla
Copy link
Author

feldenla commented Mar 2, 2017

Thanks very much for your assistance.

It is now working to expectation.

@rhetherington
Copy link

rhetherington commented Mar 2, 2017 via email

@rhetherington
Copy link

rhetherington commented Mar 2, 2017 via email

@JillElaine
Copy link
Owner

Thanks for the feedback, feldenla. Glad it's working. And thanks for the suggestions & great code, rhetherington. For some time (years now), I wished I had the focus to fix up my plugin. I too want to combine the debugging & iframe scripts into one main, unminimized scripts. There are also a couple of bug fixes that need to be included. I am not able to do this work now, but perhaps in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants