THIS ALWAYS USED TO WORK, somehow this is not working for me:
The thing that I am using to compare the password:
router.post("/login", [
check('email', 'Email is required').isEmail(),
check('password', 'Password is required').not().isEmpty()
], async (req, res) => {
const { email, password } = req.body;
const errors = validationResult(req);
if ( !errors.isEmpty() || !email || !password ) {
return res.status(401).json({err: "Authentication failure"});
};
try {
const user = User.findOne({ email: email });
if(!user) {
return res.status(401).json({err: "Invalid username or password"});
}
const isValid = await bcrypt.compare(password, user.password); //// <-------HERE!!!!!
console.log("Not reaching here")
if (!isValidPassword) {
return res.status(401).json({err: "Invalid username or password"});
}
//if valid password
res.status(200).send("SUCCESS");
} catch (error) {
res.status(500).json({err: "Internal server error"});
}
})
Can someone help me out? This is the first time I'm stuck here. Thanks in advance.
I was not using "await" while making a DB query: correction:
const user = await User.findOne({ email: email });
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.