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

前端 未结 4 1288
日久生厌
日久生厌 2020-12-01 09:31

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 logo

4条回答
  •  情话喂你
    2020-12-01 09:48

    "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.

提交回复
热议问题