node.js - Bcrypt compareSync 函数抛出非法参数错误
问题描述
我正在尝试通过在发布请求中获取用户名和密码来登录用户。我这样做是为了让每个用户都有唯一的用户名。但是当我尝试通过将请求中的密码与返回的哈希密码进行比较来检查用户是否输入了正确的密码时从数据库中,所以如果它们匹配我可以发回一个令牌我得到这个错误:非法参数:字符串,未定义。
这是代码:
router.post('/login', (req, res) => {
User.find({ username: req.body.username }, (err, data) => {
var passwordIsValid = bcrypt.compareSync(req.body.password, data.password);
if (err) return res.send(err);
if (data.length === 0 || !passwordIsValid) return res.json({ msg: "Invalid credentials." });
var token = jwt.sign({ id: user._id }, 'supersecret', {
expiresIn: 86400 // 24 hours
});
res.json({ auth: true, token: token });
});
});
解决方案
这里的代码片段我没看懂var token = jwt.sign({ id: user._id }, 'supersecret', {
,user._id
从哪里来的?此行中的更改应该可以解决,如果成功,请执行测试建议。
router.post('/login', (req, res) => {
User.find({ username: req.body.username }, (err, data) => {
var passwordIsValid = bcrypt.compareSync(req.body.password, data.password);
if (err) return res.send(err);
if (!passwordIsValid) return res.json({ msg: "Invalid credentials." });
var token = jwt.sign({ id: data._id }, 'supersecret', {
expiresIn: 86400 // 24 hours
});
res.json({ auth: true, token: token });
});
});
如果您更喜欢使用 async / await 进行测试。
router.post('/login', async (req, res) => {
const user = await User.find({ username: req.body.username })
if(!user){
return res.send({Error: 'User does not exist'})
}
if(!await bcrypt.compare(req.body.password, user.password)){
return res.send({msg: 'Invalid credentials.'})
}
const token = jwt.sign({id: user._id}, 'supersecret',{
expiresIn: 86400
})
res.json({ auth: true, token: token });
});
后来我发现这个错误而不是 user._id 它是 data._id 但它仍然没有工作原来数据是一个对象数组,因为每个用户名都是唯一的,它总是只有一个对象保存有关用户的信息给定用户名,所以为了访问用户密码,我需要写 data[0].password 而不是 data.password。
推荐阅读
- javascript - 将脚本 js 添加到 Angular 9 项目
- javascript - 通过javascript在点击时触发CSS动画
- ssl - Curl 未发送客户端证书
- python - 从模板中的 django-form 获取 html 文本输入元素
- python - 将 BQ 查询结果下载到 Python 数据框时出现 504 Deadline Exceeded 错误
- node.js - 错误:在 BoundPool.end 上多次调用池结束
- windows - 无法为非管理员用户的 PATH 找到 Windows 终端的 wt.exe
- mysql - 为什么我的 SQL 查询不适用于 JDBC Java
- sql - HIVE_PARTITION_SCHEMA_MISMATCH - 不同列之间
- python - Python Dataframe fillna,左列有值