I wanted to logout automatically from all the open tabs when logged out in one open tab.
I'm setting a jwt token to localStorage on login and removing the token when logout.
How do I use storage events to logout from all open tabs?
I wanted to logout automatically from all the open tabs when logged out in one open tab.
I'm setting a jwt token to localStorage on login and removing the token when logout.
How do I use storage events to logout from all open tabs?
You can add event listener on storage as:
window.addEventListener('storage', (event) => {
if (event.storageArea == localStorage) {
let token = localStorage.getItem('jwt_token');
if(token == undefined) { // you can update this as per your key
// DO LOGOUT FROM THIS TAB AS WELL
this.router.navigate(['/']); // If you are using router
// OR
window.location.href = '<home page URL>';
}
}
}, false);