1

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;
    }
  );
};
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Blakjay116
  • 27
  • 4
  • You've defined `findByUsername` as a function; you set `user` to a **reference** to that function instead of calling the function. Once you've fixed that your next issue will dupe [How do I return the response from an aynchronous call](https://stackoverflow.com/q/14220321/438992). – Dave Newton May 03 '23 at 19:48
  • Thanks. I adjusted it. But it still returns undefined. I even added username and password fields to specify what the username is – Blakjay116 May 04 '23 at 09:37
  • Unfortunately I am unable to see your adjustments. It’s still likely to be related to the link I posted previously. – Dave Newton May 04 '23 at 12:50

0 Answers0