【发布时间】:2021-09-21 22:25:48
【问题描述】:
我正在尝试执行我的登录功能(我正在使用 bcrypt 和 jsonwebtoken)问题是 console.log (user._id) 返回我 "new ObjectId (" 6148f043ebbaa0ab41ac8499 ")" 而不仅仅是 "6148f043ebbaa0ab41ac8499" ,创建令牌会更容易。
module.exports.login = async (req, res) => {
const { email, password } = req.body;
// Compare the req.body.password to the hashed password in DB
const user = await UserModel.findOne({ email: email });
const match = await bcrypt.compare(password, user.password);
if (match) {
try {
const user = await UserModel.findOne({ email: email });
console.log(user._id);
// Assign a token
const token = jwt.sign({ userId: user._id }, process.env.LOGIN_TOKEN, {
expiresIn: "1h",
});
console.log(token);
res.cookie("jwt", token, { httpOnly: true});
res.status(200).json({ user: user._id });
} catch (err) {
res.status(500).json(err);
}
} else {
res.status(500).json({ message: "error!!!" });
}
};
请问如何解决?
【问题讨论】: