I am using express-session module for session handling in my express.js app
My express.js application creates session for user and admin privileges.
I have req.session.user & req.session.admin
The problem I have is I don't know how to close 1 session and not the other.
You can close a session with this command: req.session.destroy()
But that destroys both sessions. I tried using req.session.user.destroy() with no luck. It is not a valid command.
So how can I destroy 1 session and not the other?
In a normal configuration, there is only one session object for each browser per site. That session object can have multiple properties, but there is only one session so you don't destroy a session just to remove a property. Further, the session is just a plain Javascript object so you can add/remove properties from it.
Since you're talking about "separate sessions", it's not entirely clear what you're trying to accomplish. You can remove individual properties from the session with the delete
operator as in:
delete req.session.admin;
This will remove the .admin
property from the session object, but leave the session itself and other properties on it intact.