I am using bycrypt for my login process and it doesn’t seem to be working. I have already stored the previously hashed password in the database so I am retrieving it from the database and then comparing it with the password but it is like my bycrpt.compare isn’t even being activated in my code. I have verified that my password and the hashed password are there, and they do work but still nothing.
Here is my code:
app.post("/login", (req, res) => {
const username = req.body.username;
const password = req.body.password;
const responseArr = [];
let passwordStatus = "";
db.query(
"SELECT password FROM users WHERE username = $1",
)
.then((res) => {
const hash = res.rows[0].password;
console.log("hash", hash);
console.log("password", password);
bcrypt.compare(hash, password, function(err, result) {
console.log("in func", res.rows[0].password)
console.log("in func", password)
if(result){
console.log("login success");
console.log(result);
passwordStatus = true;
}else{
console.log('login failed');
passwordStatus = false;
}
if (err) {
console.log("err", err)
}
})
console.log("passwordStatus", passwordStatus);
return passwordStatus;
})
.then((result) => {
if (result === true) {
const userResponse = db.query(
"SELECT user_id FROM users WHERE username = $1;",
);
return userResponse;
} else {
res.status(404);
console.log("result was false")
}
})
.then((response) => {
responseArr.push(response.rows);
res.status(200).send(responseArr);
})
.catch((error) => {
console.log("error:", error);
if (error) {
throw error;
}
});
});```
2
Answers
Funtions params need to be in following manner :-
Replace your params order.
I am using bycrypt version 5.1.1 for my login process I have already stored the previously hashed password in the database so I am retrieving it from the database and then comparing it with the password but it is like my bycrpt.compareSync() I have verified that my password and the hashed password verified and working
Here is my code: