Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logout all open tabs automatically when user logs out in one of them

I have created Login page, based on localStorage. On loading the page I have checked the value of localStorage. If I opened the web page in more than one tab and then I logout from any one of the tabs, all the remaining pages should logout automatically.

If reload/refresh means it logging out.

Please help me to run a script, when user view the page or say any other way to solve this problem.

like image 320
user1825788 Avatar asked Nov 22 '12 13:11

user1825788


People also ask

How user will logout from all open tabs automatically when user logs out in one of them in php?

localStorage. setItem('logout-event', 'logout' + Math. random()); Every other tab will get it with a listener.

How user will logout from all open tabs automatically when user logs out in one of them in asp net?

localStorage. removeItem("token_name"); Step-3: Use storage event. It will be fired automatically in all tabs when localStorage will be modified.

How do I stop multiple logins on the same browser on different tabs?

Solution 1 While login in page load check the Session["UserName"] value.. if already logged in the some user in the browser.. then redirect default page... if (Session["LoginUserName"] != null) { //redirect to default page.. like Homepage.. }


2 Answers

You can use Storage events to be notified when localStorage values are changed.

function storageChange (event) {     if(event.key === 'logged_in') {         alert('Logged in: ' + event.newValue)     } } window.addEventListener('storage', storageChange, false) 

If, for example, one of the tabs logs out:

window.localStorage.setItem('logged_in', false) 

Then all other tabs will receive a StorageEvent, and an alert will appear:

Logged in: false 

I hope this answers your question!

like image 179
Jim O'Brien Avatar answered Sep 18 '22 19:09

Jim O'Brien


"localStorage persists any saved data indefinitely on the user's computer and across browser tabs" Source

This means that if you empty / remove the login data you've set in one tab, the data will be changed in all other tabs as well.

So, if the user logs out, localStorage data changes.
Then, on your tabs, detect when a user changes focus to that tab again using the onfocus event:

function onFocus(){ //Reload Page if logged out (Check localStorage)     window.location.reload(); };  if (/*@cc_on!@*/false) { // check for Internet Explorer     document.addEventHandler("focusin", onFocus); } else {     window.addEventHandler("focus", onFocus); } 

Source

This means you won't be constantly running javascript, or reloading (a possibly large amount of) tabs at the same time.

like image 42
Cerbrus Avatar answered Sep 19 '22 19:09

Cerbrus