我在用户使用 bcrypt 登录后设置了一个 httpOnly cookie。
router.post('/login', (req, res) => {
User.findOne({email: req.body.email}, function(err, user) {
if (err) throw err;
if (user) {
user.comparePassword(req.body.password, function(err, isMatch) {
if (err) throw err;
let token = jwt.sign({ id: user._id }, process.env.SECRET_KEY, {expiresIn: '24h'});
res.cookie('token', token, {maxAge: 60*1000, httpOnly: true, secure: true})
res.status(200).json(user)
});
} else {
res.status(404).send("User not found!")
}
});
});
但问题是,在 postman 中发送请求后,cookies
选项卡中没有保存任何 cookie。我可以看到 postman 中返回的用户数据作为响应正文,还有响应头 Set-Cookie
的值为 token=eyJhbGciO...19oUxXc; Max-Age=60; Path=/; Expires=Sun, 01 Aug 2021 17:49:39 GMT; HttpOnly; Secure
,我不知道这个头是做什么的。
但是邮递员的响应 cookies
选项卡中没有 cookie。在我看到每次 cookie 作为响应出现之前,它也会显示在响应 cookies
选项卡中。 。
答案 0 :(得分:0)
解决了!我需要在开发中设置 secure=false
。