FindbyUsername 总是返回 undefined

问题描述 投票:0回答:0

我正在尝试使用护照本地策略实施登录和注销。该策略是运行一个函数来检查是否存在具有该用户名的用户,然后继续使用 passport 和 bcrypt 登录。 除了用于登录的帖子路线外,其他所有东西都有效


// passport local strategy setup
   `passport.use(
      new LocalStrategy((username, password, done) => {
        const user = du.findByUsername;
    // return error if an error found
    // if user does not exist
        if (!user) {
          console.log("user does not exist");
          return done(null, false, { message: "user does not exist" });
        }
`
    // use bcrypt to compare passwords
        `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;
        }
      );
    };
    ` 
node.js postgresql passport-local
© www.soinside.com 2019 - 2024. All rights reserved.