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.