I want to logout my application when closing last browser tab. I added @HostListener in my app component. Consoling tabcount in constructor gives exact count of opened tab but can't understand why my condition doesn't meet my requirement. Currently it's always logged me out.
app.component.ts
constructor(
private authService: AuthService
) {
console.log(tabCount.tabsCount());
}
// Handle session on browser tab close . . .
@HostListener('window:beforeunload', ['$event'])
public beforeunloadHandler($event) {
console.log(tabCount.tabsCount());
if (localStorage.getItem('rememberMe') == null){
if (tabCount.tabsCount() == 1){
console.log(tabCount.tabsCount());
this.authService.logout();
}
}
}
Logout method:
logout(): void {
$('.modal').modal('hide');
localStorage.removeItem('accessToken');
localStorage.removeItem('session_data');
this.router.navigate(['/login']);
}