In order to comply with the requirement, I need to log out the user when they close the last tab on the 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);
}
}
There seems to be inconsistency in the behavior. Sometimes the counter decreases, sometimes it doesn't. Additionally, I need to address the logic for handling refresh, closing of multiple tabs, and browser closure.
What is the best way to implement this?