#Log out user when they leave the website

15 messages · Page 1 of 1 (latest)

agile scarab
#

How can I detect and log out the user when they leave the website either by closing the tab or the browser?

strange sage
#

it's not a reliable way to log out. You may want to have another strategy, for instance you could store a unique session id in the sessionStorage => this storage being emptied on session end, next time the user open the browser, they are technically log out

#

why do you need htat in the first place?

#

also this is not Next specific, you might waznt to check more generic JS discord servers

agile scarab
strange sage
#

yeah so then store a unique session id in session storage, it should be emptied on closing the tab

#

when user access back, they will technically still have a valid login token, but they will miss the info from session storage => it means they should be logged out

#

I haven't coded that ever but it should do what you want

#

think of a key in 2 pieces: 1 is the token, 1 is what you put in the session storage

#

if user just has the token, it doesn't work anymore, it needs the part in the session storage

#

oh if I remember correctly

#

I think there are session-scoped cookies too

#

so you might not even need sessionStorage but just a correct config of your cookie that stores the token

#

not sure though