Angular 2 : Logout all open tabs automatically when user logs out in one of them -
i have created login page, based on sessionstorage. on loading page have checked value of sessionstorage. if opened web page in more 1 tab , logout 1 of tabs, remaining pages should logout automatically.
please me run script, when user view page or other way solve problem.
on click of logout button
logout() { const userloggedout = this.loginservice.changestatuslogout(this.isauthenticated.getloginstatus(), this.isauthenticated.getloggedinuserid()); if (userloggedout) { const link = ['login']; this.router.navigate(link); } } changestatuslogout(status: boolean, userid){ if (userid && status) { sessionstorage.removeitem('userid'); sessionstorage.removeitem('usergroup'); sessionstorage.removeitem('userlogedin'); return true; } } getloginstatus() { const checkstaus = sessionstorage.getitem('userlogedin'); if (checkstaus === 'true') { return this.userloggedin = true; } } getloggedinuseridgroup() { const loggedinusergroup = sessionstorage.getitem('usergroup'); return loggedinusergroup; }
you can use storage events notified when localstorage values changed.
function storagechange (event) { if(event.key === 'logged_in') { alert('logged in: ' + event.newvalue) }} window.addeventlistener('storage', storagechange, false)
if, example, 1 of tabs logs out:
window.sessionstorage.setitem('logged_in', false)
then other tabs receive storageevent, , alert appear:
logged in: false
Comments
Post a Comment