我正在尝试将Bearer令牌添加到我的POST路由中。通过邮递员提交POST请求时,得到以下输出:
{
"success": true,
"token": "Bearer undefined"
}
这是我的user.js代码:
router.post("/login", (req, res) => {
const email = req.body.email;
const password = req.body.password;
//find user by email
User.findOne({ email }).then(user => {
//check for user
if (!user) {
return res.status(404).json({ email: "user not found" });
}
//check password
bcrypt.compare(password, user.password).then(isMatch => {
if (isMatch) {
//user matched
const payload = { id: user.id, name: user.name, avatar: user.avatar }; //create jwt payload
//sign token : good for one hour
jwt.sign(
payload,
keys.SecretOrKey,
{ expiresIn: 3600 },
(err, token) => {
res.json({
success: true,
token: "Bearer " + token
});
}
);
} else {
return res.status(400).json({ password: "password incorrect" });
}
});
});
});
// @route GET api/users/current
// @desc Return current user
// @access Private route
router.get(
"/current",
passport.authenticate("jwt", { session: false }),
(req, res) => {
res.json({ msg: "Success" });
}
);
module.exports = router;
我不确定问题出在哪里。任何建议表示赞赏。
答案 0 :(得分:1)
请一次检查您为keys.SecretOrKey
获得的价值