简体   繁体   English

如何使用node.js express-session关闭单独的会话?

[英]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 我在express.js应用程序中使用express-session模块进行会话处理

My express.js application creates session for user and admin privileges. 我的express.js应用程序为用户和管理员权限创建会话。

I have req.session.user & req.session.admin 我有req.session.userreq.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() 您可以使用以下命令关闭会话: req.session.destroy()

But that destroys both sessions. 但这破坏了两个会话。 I tried using req.session.user.destroy() with no luck. 我尝试使用req.session.user.destroy()没有运气。 It is not a valid command. 这不是有效的命令。

So how can I destroy 1 session and not the other? 那么,我该如何销毁1个会话而不是另一个?

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. 此外,会话只是普通的Javascript对象,因此您可以从中添加/删除属性。

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运算符从会话中删除各个属性,如下所示:

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. 这将从会话对象中删除.admin属性,但保留会话本身及其上的其他属性。

可能对您有帮助->删除req.session.user

You can try as follows to unset session property: 您可以尝试以下操作以取消设置会话属性:

delete req.session.username;
//OR
req.session.username=null;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM