Vinayak D T
Vinayak D T

Reputation: 3

Log out from all browser tabs when one tab is logged out

I am new to angular 4 I have created one application using angular 4 where I have a login page and home page.When I log in with my credentials I will navigate to home page.

If I my application is opened in multiple tabs when I log out from one tab any click on other tab in application should redirect to login page as it happens in gmail.

Can any one please help me on this.Thanks in advance.

Upvotes: 0

Views: 4013

Answers (3)

dubadub
dubadub

Reputation: 3342

In case you store token/login info in the local storage, you can listen to changes like that:

window.addEventListener('storage', (event) => {
  // there is a problem with IE (in all other browsers event not emitted in current window) 
  // so need to check that it is the other tab that emitted event
  // and avoid infinite loop
  if (!document.hasFocus() && event.key === environment.localStorageTokenKey) {
    // do stuff here
    window.location.pathname = '/';
  }
}, false);

Upvotes: 0

Hariprakash Sambath
Hariprakash Sambath

Reputation: 169

Solution:

Please maintain a authentication token, it will help you. It is not related to Angular 4

Example:

  1. Create a token and store in the database while login
  2. Check the token for each backend call whether the token is available or not
  3. Once token is not available redirect to login page
  4. While log out remove the token from database
  5. You opened multiple tabs, log out from one tab, then try to click from another tab, it will check for the token, once the token is not available in the database it will automatically redirect to login page

This is the simple solution for your question

Upvotes: 0

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657476

You can send a request to the server to invalidate the authentication token (not related to Angular) and all tabs need to regularly check on the server if their token is still valid and log out themselves if not.

Upvotes: 0

Related Questions