如何将Express Validator 6.0.1添加到Passport.js重定向属性

时间:2019-06-23 07:20:16

标签: node.js passport.js express-validator

我在代码中拥有successRedirectfailureRedirect passport.js属性,我希望它们express-validator 6.0.1 check()的电子邮件和密码字段运行一次

我的注册页面:

<form action="/user/signup" method="post">
        <div class="form-group">
            <label for="email">E-mail</label>
            <input type="text" id="email" name="email" class="form-control">
        </div>
        <div class="form-group">
            <label for="password">Password</label>
            <input type="text" id="password" name="password" class="form-control">
        </div>
    <input type="hidden" name="_csrf" value="{{ csrfToken }}">
<button type="submit">Sign Up</button>

我的路由器:

const { check, validationResult } = require('express-validator');
   router.post("/user/signup", passport.authenticate("local.signup",
[
  check('email').isEmail(),
  check('password').isLength({ min: 5 })
],
(req, res) => {
  const errors = validationResult(req);

  if (!errors.isEmpty()) {
    return res.status(422).json({ errors: errors.array() });
  }
},
{
  successRedirect: "/user/profile",
  failureRedirect: "/user/signup",
  failureFlash: true
}));

passport.serializeUser((user, done) => {
    done(null, user.id);
});

passport.deserializeUser((id, done) => {
    User.findById(id, function(err, user) {
        done(err, user);
    });
});

我的password.js:

var passport = require("passport");
var User = require("../models/user");
var LocalStrategy = require("passport-local").Strategy;

passport.use("local.signup", new LocalStrategy({
    usernameField: "email",
    passwordField: "password",
    passReqToCallback: true
}, (req, email, password, done) => {
    User.findOne({"email": email}, (err, user) => {
        if(err) {
            return done(err);
        }
        if (user) {
            return done(null, false, {message: "Email is already in use."});
        }
        var newUser = new User();
        newUser.email = email;
        newUser.password = newUser.encryptPassword(password);
        newUser.save((err, result) => {
            if (err) {
                return done(err);
            }
            return done(null, newUser);
        });
    });
}));

我是表达验证器的新手,我在路由器中进行了尝试。但是,当我运行app.js并单击“注册”按钮时,TypeError: Cannot read property 'Symbol(express-validator#contexts)' of null.进入了终端。我在做什么错,如何在代码中添加express-validator 6.0.1?感谢您的宝贵时间。

1 个答案:

答案 0 :(得分:0)

尝试将passport.authenticate移到验证规则旁边。

const { check, validationResult } = require('express-validator');

router.post("/user/signup",
    [
        check('email').isEmail(),
        check('password').isLength({ min: 5 })
    ],
    (req, res) => {
        const errors = validationResult(req);
        if (!errors.isEmpty()) {
            return res.status(422).json({ errors: errors.array() });
        }
    },
    passport.authenticate("local.signup", {
        successRedirect: "/user/profile",
        failureRedirect: "/user/signup",
        failureFlash: true
    })
);