简体   繁体   English

删除 httpOnly cookies - 快递

[英]Delete httpOnly cookies - Express

Is it possible to delete browser cookies that are set as HttpOnly:true ?是否可以删除设置为HttpOnly:true的浏览器 cookies ?

My login endpoint is simple like this:我的登录端点很简单,如下所示:

  async login(@Ip() ipAddress, @Request() req, @Res() res: Response) {
      const auth = await this.basicAuthService.login(req.user, ipAddress);
      const cookieOptions = setTokenCookie();
      res.cookie('token', auth.token,  { httpOnly: true, expires: myDate()});
      res.cookie('refreshToken', auth.refreshToken, { httpOnly: true, expires: myDate()});
      res.send(auth);
    }

Works perfect, I call the /login endpoint in my react front end with axios完美运行,我用 axios 在我的反应前端调用 /login 端点

const res = await axios.post(`${baseUrl}/authentication/login`, { email, password }, { withCredentials: true });

So far, so good, cookies are set.到目前为止,一切顺利,cookies 都设置好了。 But I want to delete those cookies when I log out, since they are HttpOnly:true I can't delete them on frontend.但是我想在注销时删除那些 cookies,因为它们是HttpOnly:true我不能在前端删除它们。 I have tried with res.clearCookie() method but they are still in the browser.我尝试过res.clearCookie()方法,但它们仍在浏览器中。

  async logout(@Request() req, @Res() res: Response) {
      res.clearCookie('refreshToken' ,{ domain: 'localhost', path: '/', expires: new Date(0) });
      res.clearCookie('token', { domain: 'localhost', path: '/', expires: new Date(0) });
      console.log('cookies deleted');
      res.send();
    }

I thought this wasn't possible and then, I tried to login in my Facebook account and I was able to see some HttpOnly:true cookies which are deleted when logout.我认为这是不可能的,然后,我尝试登录我的 Facebook 帐户,我能够看到一些HttpOnly:true cookies 在注销时被删除。

I know this question is almost 2 years old but was the first link I found when trying to fix the same issue.我知道这个问题已经有将近 2 年的历史了,但这是我在尝试解决相同问题时发现的第一个链接。

From the Express 4.x - API Reference来自Express 4.x - API 参考

Web browsers and other compliant clients will only clear the cookie if the given options is identical to those given to res.cookie(), excluding expires and maxAge. Web 浏览器和其他兼容客户端将仅在给定选项与 res.cookie() 提供的选项相同时清除 cookie,不包括 expires 和 maxAge。

To properly delete an httpOnly cookie you must pass that as an option in the second parameter as such要正确删除httpOnly cookie,您必须将其作为第二个参数中的选项传递

res.clearCookie('token', { httpOnly: true });

I believe the same applies to sameSite , secure etc我相信这同样适用于sameSitesecure

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

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