Home  >  Forum  >  Angular
Post New Query

logout all open tabs automatically when user logs out in one of them in angular 6



Asked on October 01, 2019
logout all open tabs automatically when user logs out in one of them in angular 6



Replied on October 01, 2019
Step-1: Create JavaScript localStorage and save a token. localStorage saves a token as key value pair. It has no expiry time. It will available when we open new tab, too.

Set Item code:

localStorage.setItem("token_name", "username");

Get Item code:

var username= localStorage.getItem("token_name");

Step-2: Once you logout, remove the value from localStorage.

localStorage.removeItem("token_name");

Step-3: Use storage event. It will be fired automatically in all tabs when localStorage will be modified.

@Component({
    ------ 
})
export class AppComponent implements OnInit{

  ngOnInit() {

      window.addEventListener('storage', (event) => {
          if (event.storageArea == localStorage) {
               let token = localStorage.getItem('token_name');
               if(token == undefined) { 
                 // Perform logout
                 //Navigate to login/home
                  this.router.navigate(['/login']); 
               }
          }
      });

  }

}




Replied on October 02, 2019
thanks ............

Write Answer




©2019 concretepage.com | Privacy Policy | Contact Us