0

My client needed a mechanism in the Angular frontend of the app, like -- when user closes the browser tab or the browser itself, the user should be logged off at the organization's SSO level, which would make the user re-login, when he/she requests the app again.

I'm doing this by making use of the pagehide event, in this way:

export class HomepageComponent implements OnInit {

  ngOnInit(): void {
    this.subscribeToNativeNavigation();
  }
  
  private subscribeToNativeNavigation() {

    fromEvent(window, "pagehide").subscribe(() => {
    
        const message = "You may lose your data if you refresh now";
        window.event.returnValue = !!message;     
         
        //do the necessary session storage cleanups and other resets
        //fire an explicit logout request to the organization's SSO
          
        return message;
    });
  }
}

The need:
The above snippet works fine for browser tab or browser close, but the pagehide event seems to be getting triggered for browser refresh as well - which the client doesn't want. In other words, browser tab close (or browser close) needs to be handled as is currently, but browser refresh should not go through the above snippet (ie. user must not get logged off)

Is there a way I could achieve this while still using pagehide or I should be handling a different event? Or did I get this entirely wrong? Please advise.

Tatha
  • 131
  • 1
  • 13
  • This is simply not possible. See the answer here: https://stackoverflow.com/a/14893469/6513723 – pascalpuetz Sep 20 '21 at 22:38
  • 1
    Did you give sessionstorage a shot ? Instead of storing data in cookies, you would be storing them sessionstorage, that way they would be cleaned automaticaly after user closed tab. – millenion Sep 21 '21 at 07:06
  • Millenion -- I stand corrected. I am in fact cleaning the browser session storage (not cookies)..I updated the code snippet now. However, the need is to somehow exclude page refreshes from going through the above logic. But what I gather from that post @pascalpuetz pointed to, there is no straight forward way to identify in what way the user navigates away from the page (could very well be a page refresh). – Tatha Sep 21 '21 at 13:00

0 Answers0