[英]bcrypt.compare always returns false
I am trying to get a login function going but when I try to compare password with hashed password it always returns false我正在尝试登录 function 但是当我尝试将密码与散列密码进行比较时,它总是返回 false
This is my registerUser function that holds the hashing这是我的 registerUser function 保存散列
const registerUser = async (request, response) => {
const hashedPassword = await bcrypt.hash(request.body.password, 12)
const user = new UserModel({
username: request.body.username,
password: hashedPassword
});
try {
const databaseResponse = await user.save();
response.status(201).send(databaseResponse);
} catch (error) {
response.status(500).send({message: error.message});
}
};
This is the login function that holds the compare这是保存比较的登录 function
const login = async (request, response) => {
try{
const user = await UserModel.findOne({username: request.body.username})
if(!user) return response.status(403).send('no user with name ' + request.body.username + ' found');
const isPassValidated = await bcrypt.compare(request.body.password, user.password)
if(!isPassValidated) return response.status(403).send('wrong password');
response.status(200).send('yay');
} catch(error){
response.status(500).send({message: error.message});
}
}
When I console.log request.body.password and user.password the hashed string is equal to the unhashed string so it should return true当我 console.log request.body.password 和 user.password 哈希字符串等于未哈希字符串,所以它应该返回 true
The hashed string is the same string that is saved in the database哈希字符串与保存在数据库中的字符串相同
I just found the issue, thank you for your help.刚刚发现问题,谢谢大家帮忙。 in userSchema, password was lowercase:true which caused the issue...
在 userSchema 中,密码是小写:true 导致问题...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.