Search code examples
javascriptcssscrolluser-experienceeasing

How can you check if a user is logged into the site on one tab but not on another tab from the same browser?


GitHub recently implemented a feature whereby if you're viewing a page as a guest in one tab then log into the site from another tab, in the tab where you are still a guest, it will display the following message: enter image description here

This works the same when the tabs are in an incognito/private window.

Does the site/session variables force the client-side to check for existing cookies?


Solution

  • If this is purely out of curiosity, there are ways to easily add this. As you mentioned, when the page is opened, it would be checking the saved cookies, finding the saved SessionID and checking it with the backend to see if it is still an active session. Upon the page being closed, this sessionID would then be cleared and removed from the static list on the backend. You should be able to fill in the missing gaps from here.

    Easiest to manage cookies with PHP, calling it with javascript, if you need an idea of a direction to take. Good luck!