Hey there, I'm currently working on setting up my first login functionality using sequelize, but I'm running into some issues with hashing and comparing hashes as it always returns false. Since I am still learning, I believe I may be making a mistake in the hashing or comparison process. My database of choice is SQL.
Below is my login code snippet that utilizes express session and sequelize:
'processLogin': (req, res) => {
db.User.findOne({
where: {
email: req.body.email
}
})
.then(async user => {
var eSession = req.session.userLogin;
let checkPass = await bcrypt.compare(req.body.password, user.password);
console.log(checkPass);
if(checkPass){
eSession = user;
res.send("Success");
}else{
res.render('login', {errors: [{
msg: "Incorrect password"
}]});
}
if(user == undefined){
res.render('login', {errors: [{
msg: "User not found, please Register"
}]);
}
});
}
And here is the portion where I hash passwords during registration:
'save': async (req, res) => {
var dataArr = [req.body.fullname, req.body.email, req.body.number, bcrypt.hashSync(req.body.password, 10), bcrypt.hashSync(req.body.repassword, 10)];
let errors = validationResult(req);
if(errors.isEmpty()){
db.User.create({
full_name: dataArr[0],
email: dataArr[1],
phone_number: dataArr[2],
password: await bcrypt.hash(dataArr[3], 10),
confirm_password: await bcrypt.hash(dataArr[4], 10)
})
.then(users => {
res.send("Success!!");
})
}else{
res.render('register', { errors: errors.errors });
}
}