javascript - 删除 httpOnly cookies - Express

标签 javascript node.js typescript express cookies

是否可以删除设置为 HttpOnly:true 的浏览器 cookie?

我的登录端点很简单:

  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);
    }

工作完美,我用 axios 在我的 react 前端调用/login 端点

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

到目前为止,一切顺利,cookie 已设置。但是我想在注销时删除这些 cookie,因为它们是 HttpOnly:true 我无法在前端删除它们。我已尝试使用 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();
    }

我认为这是不可能的,然后我尝试登录我的 Facebook 帐户,我能够看到一些 HttpOnly:true cookie,这些 cookie 在注销时被删除。

最佳答案

我知道这个问题已有将近 2 年的历史,但它是我在尝试解决同一问题时发现的第一个链接。

来自Express 4.x - API Reference

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.

要正确删除 httpOnly cookie,您必须将其作为第二个参数中的选项传递

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

我相信这同样适用于 sameSitesecure

关于javascript - 删除 httpOnly cookies - Express,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64723114/

相关文章:

node.js - 如何使用 aws-sdk Javascript 获取 EC2 公共(public) ip

具有两个属性或两个属性的 Typescript 接口(interface)

Angular 4 : 'Property ' includes' is missing in type' for custom data type

typescript - 链接 redux-actions 和 redux-promise-middleware

javascript - 如何测试模块中的 OpenCV nodejs 绑定(bind)代码?

javascript - 替换特定类声明中的字符

node.js - Adobe CEP 扩展中的访问 Node

node.js - 如何使用node js实时改变内容?

javascript - Collection View 未渲染项目 View

javascript - 双引导简单侧边栏