我在授权登录时发送cookie,但在查看浏览器中存储的cookie时不会显示。发送cookie的代码如下:
app.post('/login', async (req, res) => {
const { email, password } = req.body;
const user = await User.findOne({email})
if (!user) {
return res.status(404).json({message: 'User not found'});
}
if (!bcrypt.compareSync(password, user.password)) {
return res.status(401).json({message: 'Incorrect password'});
}
jwt.sign({email:user.email, id:user._id}, process.env.JWT_SECRET, {}, (err, token) => {
if (err) {
return res.status(500).json(err);
}
res.cookie('token', token).json(user);
});
});
http响应如下所示:
HTTP/1.1 200 OK
X-Powered-By: Express
Access-Control-Allow-Origin: http://localhost:5173
Vary: Origin
Access-Control-Allow-Credentials: true
Set-Cookie: token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6Im1hcmNvcy5zZWNvLmFuZGVyc29uQGdtYWlsLmNvbSIsImlkIjoiNjUyYmUxODk4NGE4MjE2MWM3NWY1MTcwIiwiaWF0IjoxNjk3NDY2NTU1fQ.TbzDhQgQydtppUBo4NfMWmYhtJpZWUxrvWRv4Dqqmas; Path=/
Content-Type: application/json; charset=utf-8
Content-Length: 175
ETag: W/"af-QCiPuvEnfeYRFPIjeqReewm0cLM"
Date: Mon, 16 Oct 2023 14:29:15 GMT
Connection: keep-alive
Keep-Alive: timeout=5
cookie在响应中,但没有被存储,有人知道为什么吗?
我的后端在端口4000,前端在5173,以防有帮助
我尝试过进入隐姓埋名模式,我的代码也有一些变化,比如添加一条路径,但没有成功。