Reputation: 672
I'm learning to use the "client-sessions" package in Node.js, with Express. Everything seems to be working well. But I'm wondering, is there is a way to have it call a function if a session expires?
I'm setting things up so that the session only stores a unique ID. Then outside of the session I store all the user data and anything else I need. That way it keeps my cookies and session info very small. But I want to know when a session has expired so that I can clear any info that doesn't need to be kept after the session is gone.
But I have not been able to find any details on how to do this. Am I thinking about this in the wrong way? If I store everything in the session it will try to store it all in the cookie as well won't it?
thanks in advance.
Upvotes: 0
Views: 2680
Reputation: 707396
Making my comments into an answer since it appears to have helped you solve your issue...
If you use a session manager like express-session
that only stores a session ID in the browser cookie and keeps a session object server-side in the session store, then you can just store your data directly in the session object and when the session expires, the session manager will just automatically clean up the session (including your data in the session). Then, you don't have to worry about when it expires as things are just managed for you automatically.
express-session
has the ability to look up a given session in the session store.get(sessionID, callback)
where store
is the session store object you're using. To use that, you need to have previously saved the sessionID for a given user that you want to look up.
Upvotes: 1