是否可以删除设置为 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 年的历史,但它是我在尝试解决同一问题时发现的第一个链接。
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 });
我相信这同样适用于 sameSite
、secure
等
关于javascript - 删除 httpOnly cookies - Express,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64723114/