0

Is it possible to auto logout a user from all opened tabs if that user sign out from one of the open tab in the browser in buddypress/wordpress.

Thanks!

2 Answers 2

1

So this depends a bit on what you mean:

The browser shares one session over all tabs/windows so you're either logged in or out everywhere. So when you log out you are actually logged out in all tabs. You just don't notice until you try to go somewhere else because, well, the open page doesn't just magically disappear from the browser view. But you can't go anywhere else from now on.

What you might also mean is that as soon as you log out in one tab all other pages open in other tabs/windows "close" immediately. This might be somewhat doable via JS, but may I ask what the point of this is? The user has already opened the page and seen it. Taking it away now doesn't really change anything anymore, right?

1
  • 1
    +1 but I can see the value of indicating you are "disconnected" just for UX sake Commented May 3, 2017 at 6:11
1

we 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
1
  • Its working in Incognito mode also!! Commented May 3, 2017 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.