Answer the question
In order to leave comments, you need to log in
How to clear localStorage after closing all tabs with a site?
I want to implement "Remember me" functionality using JWT.
The plan is this: the JWT is written to localStorage, and if the user does not check the "Remember me" checkbox, then after closing all the tabs of the site, localStorage overwrites the entry with the token. How can I implement this?
Answer the question
In order to leave comments, you need to log in
Every 5 seconds, update the authorization data in the storage, and consider all entries older than 15 seconds as invalid. Accordingly, if the user closed all tabs, then the authorization data stopped updating and quickly became outdated.
PS This is just one of the ways. Also you can use sessionStorage.
You won’t get a 100% guarantee by local means, onbeforeunload may not be executed for various, albeit rare, reasons (for example, a process was killed, or a problem with the Internet, or an unfortunate coincidence when waking up from sleep, this is especially true for mobile browsers), so the key issue of the task is - detect the closing of tabs, which can only be done on the server side by holding the websocket connection (the server counts how many connections are open to which user and on the last disconnect it marks that the session has ended).
In this case, cleaning up localstorage will also have to be done when the page is opened, if the server marked for itself that the session was closed and outdated
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question