我正在尝试验证用户身份。我想在服务器的响应中设置身份验证标头,然后可以由axios插件获取。
登录功能
// POST /users/login
router.post('/users/login', async (req, res) => {
try {
const body = _.pick(req.body, ['email', 'password']);
const user = await User.findByCredentials(body.email, body.password);
const token = await user.generateAuthToken();
req.session['token'] = 'Bearer ' + token;
req.session['user'] = user;
res.set('Authorization', 'Bearer ' + token);
res.send(user);
} catch (err) {
res.status(401).json({ message: "Nutzerdaten sind nicht korrekt" });
}
});
Axios插件
$axios.onResponse(res => {
console.log(res.headers)
if (res.headers.authorization) {
store.commit('auth/SET_TOKEN', res.headers.authorization)
}
return res
})
Console.log(res.headers)输出
{ 'x-powered-by': 'Express',
vary: 'Origin',
'access-control-allow-credentials': 'true',
'content-type': 'application/json; charset=utf-8',
'content-length': '2',
etag: 'W/"2-l9Fw4VUO7kr8CvBlt4zaMCqXZ0w"',
date: 'Wed, 31 Jul 2019 07:30:48 GMT',
connection: 'keep-alive' }
为什么我的响应中没有授权标头,尽管它显示在浏览器的开发工具的“网络”标签中?