使用Passport.js进行密码自动验证

时间:2019-07-04 04:42:55

标签: node.js passport.js

我正在使用passport.js npm软件包对Node.js进行身份验证。我已经有本地身份验证设置。我还希望为我的应用提供基于密码的身份验证。

router.post("/signup-user",passport.authenticate("user-signup",{
    successRedirect:"/",
    failureRedirect:"/signup"
}),function(req,res){
    console.log('Route Fired')
})

Passport.js设置

passport.use('user-signup', new LocalStrategy({
            passwordField: 'pin',
            passReqToCallback: true // allows us to pass back the entire request to the callback
        },
        function(req,pin, done) {
            // we are checking to see if the user trying to login already exists
            pool.query("select * from Users where pin = " + pin , function(err, rows) {

                if (err)
                console.log(err);
                req.flash('signupMessage', err);
                if (rows.length) {
                    return done(null, false, req.flash('signupMessage', 'That Pin is already taken.'));
                } else {

                    // if there is no user with that pin
                    // create the user
                    var newUserMysql = new Object();


                    bcrypt.hash(pin, 10, function(err, hash) {


                        newUserMysql.pin = hash; // use the generateHash function in our user model

                        var insertQuery = "INSERT INTO Users ( pin ) values ('"  + hash + "')";

                        pool.query(insertQuery, function(err, rows) {
                            console.log(err)
                            console.log(insertQuery)
                            req.flash('signupMessage', err);
                            newUserMysql.id = rows.insertId;

                            return done(null, newUserMysql);
                        });

                    })

                }
            });
        }));

POST路由以failreRedirect重定向到/signup,但是我不确定为什么会这样。

0 个答案:

没有答案