Node.js 节点JS,快速会话,从客户端浏览器中删除cookie
我使用的应用程序已经有了自己的基础设施。任务是防止用户在多个浏览器中登录。我们的应用程序只有一个应用程序架构,所以理想情况下用户应该只在一个浏览器选项卡中工作。我有个问题。我无法从客户端删除cookie 一、简要介绍 应用程序设置: 服务器:NodeJS 港口:8083 客户:VueJS 端口:8088 我使用模块Node.js 节点JS,快速会话,从客户端浏览器中删除cookie,node.js,express,session,cookies,Node.js,Express,Session,Cookies,我使用的应用程序已经有了自己的基础设施。任务是防止用户在多个浏览器中登录。我们的应用程序只有一个应用程序架构,所以理想情况下用户应该只在一个浏览器选项卡中工作。我有个问题。我无法从客户端删除cookie 一、简要介绍 应用程序设置: 服务器:NodeJS 港口:8083 客户:VueJS 端口:8088 我使用模块express session初始化服务器端的会话机制,并向客户端发送cookie。客户端尚未设置cookies 二,。详情: 服务器的根文件是index.js 我在其中执行以下操作:
express session
初始化服务器端的会话机制,并向客户端发送cookie。客户端尚未设置cookies
二,。详情:
服务器的根文件是index.js
我在其中执行以下操作:
express
模块:cors
模块:cors
设置:快速会话
模块:express.Router()插入路由
router.post()接收客户端查询
req.session.destroy
删除会话数据,并期望浏览器注销用户从特定浏览器和cookie中清除res.clearCookie
方法中使用显式cookie的路径指示,如上所示。
那没用{path:'/'}
。也不管用req.session=null
:删除请求会话
也不起作用那么,我该如何解决这个问题呢?我该怎么办?您是否尝试过通过将cookie设置为null来删除该cookie?也就是说,您正在处理一个名为Views的cookie。您可以使用
req.session.Views=null删除cookie
而不是这样做
req.session.destroy(err => {
if (err) {
return res.send({ error: 'Logout error' })
}
req.session = null
res.clearCookie(SESS_NAME, {path: '/'})
return res.send({ 'clearSession': 'success' })
})
您可以设置会话cookie的名称,并将其设置为null,即
req.session.sid= null
这将从客户端浏览器中删除cookie
req.session.destroy(err => {
res.clearCookie("session-cookie-name", { path: "/" });
});
添加.send('cleared cookie')
可使我的浏览器清除其命名cookie的缓存
const logOutRequest = (req, res) => {
req.session.destroy((err) => {
res.clearCookie("notcookie").send('cleared cookie');
});
};
哦,您能指定我应该在哪里写这行吗?当你说视图时,你是指会话设置中的
name:SESS_name
,即我必须写入req.session.SESS_name=null
?不幸的是,它不起作用。会话不清楚。行req.session.SESS_NAME=null
仅在会话存储中添加新字段。它看起来是这样的:Session{cookie:{path:'/',_expires:2019-10-10T21:08:28.940Z,originalMaxAge:2880000,httpOnly:true,secure:false,sameSite:false},userId:1,blocked:0,status:'lambda',SESS_NAME:null}
很抱歉,我看到您的会话名称为'sid',请再次尝试将其设置为null,即req.session.sid=null。在第req.session.sid=null
行中显式添加了SESS_名称,得到了与前面相同的结果。会话存储中的新行:session{cookie:{path:'/',u expires:2019-10-10T21:38:26.600Z,originalMaxAge:2880000,httpOnly:true,secure:false,sameSite:false},userId:489,login:'glebtregubov',密码:'10293221',阻塞:0,状态:'392',sid:null}
所以没有任何更改
const router = express.Router()
const EIGHT_HOURS = 1000 * 60 * 60 * 8
const {
SESS_NAME = 'sid',
SESS_LIFETIME = EIGHT_HOURS,
SESS_SECRET = 'test',
NODE_ENV = 'development'
} = process.env
const IN_PROD = NODE_ENV === 'production'
router.use(session({
name: SESS_NAME,
resave: false,
saveUninitialized: false,
secret: SESS_SECRET,
cookie: {
maxAge: SESS_LIFETIME,
sameSite: false,
// Must have HTTPS to work 'secret:true'
secure: IN_PROD
}
}))
req.session.destroy(err => {
if (err) {
return res.send({ error: 'Logout error' })
}
res.clearCookie(SESS_NAME, {path: '/'})
return res.send({ 'clearSession': 'success' })
})
router.use(session({
name: SESS_NAME,
resave: false,
saveUninitialized: false,
secret: SESS_SECRET,
cookie: {
path: '/',
maxAge: SESS_LIFETIME,
sameSite: false,
// Must have HTTPS to work 'secret:true'
secure: IN_PROD
}
}))
req.session.destroy(err => {
if (err) {
return res.send({ error: 'Logout error' })
}
req.session = null
res.clearCookie(SESS_NAME, {path: '/'})
return res.send({ 'clearSession': 'success' })
})
req.session.destroy(err => {
if (err) {
return res.send({ error: 'Logout error' })
}
req.session = null
res.clearCookie(SESS_NAME, {path: '/'})
return res.send({ 'clearSession': 'success' })
})
req.session.sid= null
req.session.destroy(err => {
res.clearCookie("session-cookie-name", { path: "/" });
});
const logOutRequest = (req, res) => {
req.session.destroy((err) => {
res.clearCookie("notcookie").send('cleared cookie');
});
};