How to keep track when session expires in express session
Hello guys, sorry to disturb you all; can someone explain how can I do in order to keep track when a session expires , for e,g after 5sec without refreshing the web-page
12 Replies
The session expires after 5sec but I want to be redirected to kind of a "time-out" page because the user don't know that the session has expired
how do you know that the session expires in 5 seconds?
and why do you want to send users to a page every 5 seconds?
I'm going to assume you mean 5 seconds as an example that's easy to test. There's a couple of common ways: Either you redirect on the first failed request after the session expires, or you keep track of the session time in both the frontend and the backend.
For the first option, usually that's fine because the user doesn't really care whether the session is still alive if they're not doing anything with the site.
For the second option, the reason you keep track of it on both ends is that generally speaking once a page is loaded or an api call made and responded to, there's no further connection to the client. Any action by the user will create a new connection, but the server has no path to that user. You track the time in the browser and handle the expiration in the frontend because you can have continuously running code there.
the very simple way is to send a timestamp of expiry along to the frontend so that you can set a timeout of the appropriate length and do what you need to do on the frontend
but like I said, usually you just do the first option and not really worry about it
the only time I'd use the second option, is if I was using a token that has an expiring access token and a refresh token
I set the maxAge of cookie to 5000ms, it's for testing
Yep I see, I will try it out and revert back, thanks !
There is something known as "keep-alive" , I think it is some kind of attribute in headers, do that have anything to play with what you mentioned please
In that is a similar concept but no more than that
thats for http 1.1, to keep the tcp connection alive so it doesnt have to do all the dns and ssl/tsl handshake stuff again
that wont do anything for you
yep I see, so I can just ignore the "keep-alive" thing ?
yeah
by the way, I was able to do the session management session !!
I don't know if it is efficient but it worked and I understand how things work :c
I use the first method where we have invalid request... seems easier for now
depends... but for this, you can ignore it
dont forget that the session can be terminated in multiple ways:
- client clears the cookies
- the client doesnt communicate to the site for longer than the timeout
- the client has an invalid session token
- the server is restarted/restored/reverted
- an automatic task clears "useless" cookies
yep I see, what is the last point, for the automatic clask, didn't know it exist
im not sure if it exists for express js, but it does for other languages