I am trying to implement logging in and logging out with passport local strategy. The strategy is to run a function to check if a user with that username exists, then proceed to use passport and bcrypt to log in. Every other thing works except the post route for loggin in
passport.use(
new LocalStrategy((username, password, done) => {
const user = du.findByUsername;
if (!user) {
console.log("user does not exist");
return done(null, false, { message: "user does not exist" });
}
const match = bcrypt.compare(password, user.pword);
if(!match) {
console.log("Incorrect password");
return done(null, false, { message: "Incorrect password" });
}
console.log("success");
return done(null, user);
})
);
Routes:
router.post(
["/users/login", "/users/:id"],
passport.authenticate("local", {
failureRedirect: "/users/login",
}),
// redirect to see all prdoucts upon login
db.getAllProducts
);
module.exports = router;
Here is the findbyUsername that always retruns undefined Note that in the database, the password is stored in the pword column
const findByUsername = (name) => {
pool.query(
"SELECT * FROM users WHERE username = $1",
[name],
(error, results) => {
if (error) {
throw error;
}
return results.rows;
}
);
};