FindbyUsername 总是返回 undefined"/>
FindbyUsername 总是返回 undefined
我正在尝试使用护照本地策略实施登录和注销。该策略是运行一个函数来检查是否存在具有该用户名的用户,然后继续使用 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 = bcryptpare(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;
}
);
};
`
回答如下:
更多推荐
FindbyUsername 总是返回 undefined
发布评论