PassportJS验证功能未运行

时间:2019-02-24 13:41:06

标签: express passport.js passport-local

我有一个local-signup Passport.js策略,在该策略中,用户提交注册表单,然后在提交时运行一系列检查,以确保用户提交的电子邮件地址不在数据库中,服务条款标志设置为true,并且密码字段匹配。目前,当提交表单时没有填写完整的表单时,会触发failureRedirect,但是永远不会调用function(req, email, password, done) {路径。为什么会这样,如何解决我的护照设置?

路线:

siteRoutes.route('/sign-up')
    .get(function(req, res){
        res.render('pages/site/sign-up.hbs', {
            error: req.flash('error'),
            csrfToken: req.csrfToken()
        });
    })
    .post(passport.authenticate('local-signup', {
        successRedirect: '/app/setup/users',
        failureRedirect: '/sign-up'
    }));

*

*策略:**

passport.use('local-signup', new LocalStrategy({
    passReqToCallback: true,
    usernameField: 'email'
}, function(req, email, password, done) {
    console.log("Signup check has begun")
    console.log(e)

    models.User.findOne({
        where: {
            email: email
        }
    }).then(function(existingUser) {
        console.log("check existing user")
        //If user already exists in system
        if (existingUser){
            return done(null, false, req.flash('error', 'Email already exists.'));
        }
    //If password does not match confirm password field
        else if (req.body.tosAcceptance === undefined) {
            console.log("TOS Not Checked")
            done(null, false, req.flash('error', 'You must agree to our Terms of Service and Privacy Polcy. Please read and check the box if you agree.'));
        } else if (password !== req.body.confirmPassword) {
            console.log("Password does not match")
            done(null, false, req.flash('error', 'Passwords do not match.'));
        } else {

        }
    }).catch(function(e){
        console.log(e)
    })

我从没见过console.log("Signup check has begun"),这表明当出现错误时,这部分永远不会运行。但是,如果所有字段均已填写,则将运行此部分。不知道为什么会这样。

0 个答案:

没有答案