无法删除Express中的Cookie [英] Can't delete cookie in express

查看:99
本文介绍了无法删除Express中的Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常简单。我在 / user / login 路由中设置了这样的Cookie:

  if(rememberMe){
console.log('登录将被记住。');
res.cookie(用户,userObj,{签名:true,httpOnly:true,路径:‘/’});
}
else {
console.log(登录将不会被记住。);
}

我已经为cookie解析器设置了秘密:

  app.use(cookieParser('shhh!')); 

非常基本的东西。只要我能够检索存储在cookie中的任何内容,一切都运转良好:

  app.use(function(req ,res,next){
if(req.signedCookies.user){
console.log('Cookie存在!');
req.session.user = req.signedCookies.user;
}
else {
console.log('未找到cookie。');
}

next();
}) ;

此中间件先于其他任何东西被调用,因此为了论证 Cookie存在!如果Cookie有效,则总是在控制台中登录。



问题是当我尝试删除Cookie时。我试过 res.clearCookie('user') res.cookie('user','',{过期:新Date() }),而我尝试传递相同的标志(我将其传递给中的 res.cookie() / user / login )。我尝试使用这些方法的组合,但是没有任何效果。



目前,我唯一能够清除Cookie(并且不会收到 Cookie存在!日志消息)是通过清除我的浏览器历史记录。这是我的登出路线的样子:

  route.get('/ user / logout',function(req,res,下一个){
res.clearCookie('user');
req.session.destroy();
util.response.ok(res,'Successfully log out。');
});

似乎我什至无法修改cookie值;我把



res.cookie('user',{},{sign:true,httpOnly:true,path:'/'})



,但cookie值保持不变。

解决方案

很长一段时间之后,我意识到我在尝试清除Cookie时,前端没有将cookie发送到终点……



在服务器上:

 功能注销(req,res){
res.clearCookie( 'mlcl');
return res.sendStatus(200);
}

在前端,

  fetch('/ logout',{方法:'POST',凭据:'same-origin'})


添加了凭据:'same-origin'才使clearCookie为我工作。如果未发送cookie,则没有任何清除。



我希望这会有所帮助。我希望我早些找到了...


Pretty simple. I set a cookie like so in my /user/login route:

if (rememberMe) {
    console.log('Login will remembered.');
    res.cookie('user', userObj, { signed: true, httpOnly: true, path: '/' });
}
else {
    console.log('Login will NOT be remembered.');
}

I've already set my secret for cookie-parser:

app.use(cookieParser('shhh!'));

Pretty basic stuff. Everything is working great insofar as I'm able to retrieve whatever I stored in the cookie:

app.use(function (req, res, next) {
    if (req.signedCookies.user) {
        console.log('Cookie exists!');
        req.session.user = req.signedCookies.user;
    }
    else {
        console.log('No cookie found.');
    }

    next();
});

This middleware is called before anything else, so for the sake of the argument "Cookie exists!" is always logged in my console if the cookie is valid.

The problem is when I try to delete the cookie. I've tried res.clearCookie('user'), res.cookie('user', '', { expires: new Date() }), and I've tried passing in the same flags (that I pass to res.cookie() in /user/login). I've attempted to use combinations of these methods, but nothing has worked.

Currently, the only way I am able to clear the cookie (and not receive the "Cookie exists!" log message) is by clearing my browser history. Here is what my logout route looks like:

route.get('/user/logout', function (req, res, next) {
    res.clearCookie('user');
    req.session.destroy();
    util.response.ok(res, 'Successfully logged out.');
});

It seems as though I can't even modify the cookie value; I put

res.cookie('user', {}, { signed: true, httpOnly: true, path: '/' })

in my logout route, but the cookie value remains unchanged.

解决方案

I realized after a long and annoying time that my front end was not sending the cookie to the end point were I was trying to clear the cookie...

On the server:

function logout(req, res) {
  res.clearCookie('mlcl');
  return res.sendStatus(200);
}

And on the front end,

fetch('/logout', { method: 'POST', credentials: 'same-origin' })

adding the "credentials: 'same-origin'" is what made the clearCookie work for me. If the cookie is not being sent, it has nothing to clear.

I hope this helps. I wish I had found this earlier...

这篇关于无法删除Express中的Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆