As per the requirement, I have to log out the user when he closes the last tab on a browser.
ngOnInit() {
let counter: any = this.cookieService.get('screenCounterCookie');
counter ? ++counter : (counter = '1');
this.cookieService.set('screenCounterCookie', counter);
}
@HostListener('window:beforeunload', ['$event'])
ngOnDestroy() {
let counter: any = this.cookieService.get('screenCounterCookie');
if (counter > 1) {
--counter;
this.cookieService.set('screenCounterCookie', counter);
} else {
this.cookieService.delete('screenCounterCookie');
window.open(environment.cognitoLogoutURL);
}
}
The behaviour is erratic. Sometimes it reduces the counter, sometimes it doesn't. Also, I have to handle the refresh, multiple tabs close and browser close logic here.
How can I implement it?