0

I can invalidate session when i close the tab or windows of browser, but when i click to reopen the closest tab it loads me the recent page as it is.

I want to redirect to login page if the reopen is clicked, I clear the cookies onUnbeforeUnload but it doesn't work.

here is my code:

`$(window).on("beforeunload", function(e) {
var aCookies=document.cookie.split('; ');
        for (var i = 0; i < aCookies.length; i++) {
            var cookie = aCookies[i];
            var eqPos = cookie.indexOf("=");
            var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
            document.cookie = name + '=0; expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/';


        }  
window.location.href = "/safe-unsecured/logout";`
} 

and on load of the page i clear cookies;

Any help would be appreciated

hehofr
  • 21
  • 2
  • 1
    please share what you have tried so far. – full-stack Aug 15 '18 at 15:51
  • Possible duplicate of [Clear cookies on browser close](https://stackoverflow.com/questions/1783302/clear-cookies-on-browser-close) – Tschallacka Aug 15 '18 at 16:16
  • What do you mean by "reopen the closest tab"? Are you just talking about bringing a background tab into focus? If so you could use `window.onblur` and `window.onfocus` to detect this (but you should *definitely* not be clearing the session when the user just switches to a different browser tab, that's not normal or expected behavior.) – Daniel Beck Aug 15 '18 at 16:26
  • Also be careful if using `window.onfocus`, as it fires more frequently than you might expect (after an `alert()` is closed, for example). – Daniel Beck Aug 15 '18 at 16:31
  • Reopen the closest tab is an option on chrome with shortcut Ctrl+Maj+T – hehofr Aug 15 '18 at 16:33
  • OK -- thanks for clarifying. That option reopens the most recently closed window or tab, not the "closest" tab, and behaves the same as opening the page in any window. You can't redirect the browser or perform any other asynchronous action in `onbeforeunload`; that event also fires during normal site navigation (so even if it worked, it'd be clearing your cookies every time the user follows a link within the site.) – Daniel Beck Aug 15 '18 at 16:42
  • the problem is when i close the windows or just the tab and reopen it in another it open me the login page, but when i reopen it from chrome it loads me the page recent page with sessionTimeOutMessage? Can it be a problem of Cache ? – hehofr Aug 15 '18 at 16:47

1 Answers1

1

browsers will not load a new page on closing or browsing away from a page. That logout page will never be requested.

Also, as long as the browser is not restarted, the session will exist in the browser. A session is not invalidated by a tab closing, but by the entire browser closing. The only way to really achieve what you want is having your cookies live for one minute and have a timer running that updates the cookie expiry every 10 seconds so it doesn't expire as long as the page is open, or the tab isn't re-opened within a minute.

This is an example of the update code. You'll need to add logic yourself that checks for the existance of the cookie and everything else you need for validation.

window.myCookieValueThatIReadBefore = 'Hello';
  window.setInterval(function() {
     setCookie('cookie_name', window.myCookieValueThatIReadBefore,1)
  }, 1000*10);
  function setCookie(cname, cvalue, exminutes) {
      var d = new Date();
      d.setTime(d.getTime() + (60*1000));
      var expires = "expires="+ d.toUTCString();
      document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
  }
Tschallacka
  • 27,901
  • 14
  • 88
  • 133