Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I close separate sessions using node.js express-session?

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?

like image 282
Wee Wee Avatar asked Oct 29 '25 02:10

Wee Wee


1 Answers

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.

like image 73
jfriend00 Avatar answered Oct 30 '25 22:10

jfriend00



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!