[英]I am having a problem with bcrypt compare. I succed to hash password on Register page, but I not to compare successfully on login page. Please help :)
这是注册表单,我得到哈希密码:
getAllUsers().then((res) => {
let user = res.data.find(
(u) => u.ugovor === ugovor && u.merilo === merilo
);
if (user) {
setUser(user) {
bcrypt.genSalt(10, (err, salt)=> {
bcrypt.hash(loggedIn.password, salt, (err, hash) => {
postUser(loggedIn.id, hash, password, email).then((res) => {
user = res.data;
setPassword(prev => [...prev, hash]);
//loggedIn.password = hash;
setEmail(prev => [...prev, user]);
history("/login");
setError("");
console.log("uspesna registracija");
})
})
})
} else {
setError("Не постоји купац са унесеним бројем Уговора и мерила");
return 0;
}
});
}}>
这是登录表单,everytnihg 有效,但比较散列密码:
getAllUsers().then((res) => {
let user = res.data.find(
(u) => u.ugovor === ugovor && u.password === password
);
const doesPasswordMatch = bcrypt.compareSync(password, user.password);
if(!doesPasswordMatch) {
console.log('compare not success');
return 0;
} 别的 {
console.log('compare success')
} 如果(用户){
setLogIn(true);
setUser(user);
history("/profile");
console.log("uspesno ulogovan");
} else {
setError("Broj Ugovora i lozinka se ne podudaraju");
}
});
}}
我尝试了比较,比较同步,等待,即使这样,但没有任何效果,它不会识别散列密码:
bcrypt.compare(password, user.password, function(err, result) {
// result == true
});
试试这个登录。 请记住放弃一些饲料。
getAllUsers().then((res) => {
// look for user
let user = res.data.find((u) => u.ugovor === ugovor);
if (user) {
// user is found
const hashedPassword = u.password; // get stored hashed password
// compare passwords
const doesPasswordMatch = bcrypt.compareSync(password, hashedPassword, (err, result) => {
if (err) console.log(err);
// result == true
});
if (doesPasswordMatch) {
// correct password
console.log("compare success");
setLogIn(true);
setUser(user);
history("/profile");
console.log("uspesno ulogovan");
} else {
// wrong password
console.log("compare not success");
return 0;
}
} else {
// user is not found
setError("Broj Ugovora i lozinka se ne podudaraju");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.