How can I log out on close but not on refresh?


#1

Is there any way to logout the user when they close the tab or window but not on refresh?

Using window.onunload is easy enough but i’m struggling to come up with a hook that I can cancel the logout on refresh. Detecting shortcuts is do-able but won’t catch the refresh button click.

Any ideas?


Create random id for a non logged in user
#2

You might be able to leverage sessionStorage somehow, since it’s destroyed on window/tab close but not on browser refresh.


#3

Yeah I couldn’t crack it reliably… The best I could come up with was setting a sessionStorage on login and then when the page loads again log out the user if no session token is there… but that is not super secure and also has a flash of logged in/out.