Passport JS successRedirect在Node.js中挂起

时间:2018-06-06 08:05:19

标签: javascript node.js express passport.js

我使用的是护照local-signup,可以通过表单创建用户,并使页面成功重定向到我指定的页面。

目前我的问题是,重定向页面只是挂起。我看到还有其他人经历过类似的事情,但看着我有什么我无法弄清楚为什么我的例子悬而未决。

我首先对表单进行一些简单的验证,如果可以,我继续使用护照配置:

app.post('/signup', function(req, res, next) {
  // Capture form details for when validation fails so can repopulate
  var form = {
    first_name: req.body.first_name,
    last_name: req.body.last_name,
    email:  req.body.email
  }

  var first_name = req.body.first_name;
  var last_name = req.body.last_name;
  var email = req.body.email;
  var password = req.body.password;
  var password_confirmation = req.body.password_confirmation;

  // Validation
  req.checkBody('first_name', 'First Name is required').notEmpty();
  req.checkBody('last_name', 'Last Name is required').notEmpty();
  req.checkBody('email', 'Email is required').notEmpty();
  req.checkBody('email', 'Email is not valid').isEmail();
  req.checkBody('password', 'Password is required').notEmpty();
  req.checkBody('password_confirmation', 'Passwords do not match').equals(req.body.password);

  var errors = req.validationErrors();

  if (errors) {
    res.render('home', {
      errors: errors,
      form: form
    });
  }
  else {
    passport.authenticate('local-signup', {
      successRedirect : '/members', // redirect to the secure profile section
      failureRedirect : '/', // redirect back to the signup page if there is an error
      failureFlash : true // allow flash messages
    })(req, res, next);

  }
});

local-signup

passport.use('local-signup', new LocalStrategy({
        // by default, local strategy uses username and password, we will override with email
        usernameField : 'email',
        passwordField : 'password',
        passReqToCallback : true // allows us to pass back the entire request to the callback
    },
    function(req, email, password, done) {
      // asynchronous
      // User.findOne won't fire unless data is sent back
      process.nextTick(function(callback) {
        // we are checking to see if the user trying to sign up already exists
        // User.findOne declared in User Model
        User.findOne(email, function(err, isNotAvailable, user) {
          if (err) return done(err);

          // check to see if theres already a user with that email
          if (isNotAvailable == true) {
            return done(null, false, { message: 'That email is already taken.' });
          } else {

            newUser = new Object();
            newUser.email = email;
            newUser.password = bcrypt.hashSync(password, 10);

            pool.query('INSERT INTO users(email, password) VALUES($1, $2) RETURNING *', [newUser.email, newUser.password], function (err, result) {
            if(err){
              console.log(err);
              return console.error('error running query', err);
            }
              newUser.id = result.rows[0].id;
              return done(null, newUser);
            });
          } // isNotAvailable
        }); //User.findOne
      });
    }));

1 个答案:

答案 0 :(得分:0)

尝试这种方式:

passport.authenticate('local', { 
      successRedirect : '/members', 
      failureRedirect : '/', 
      failureFlash : true 
})(req, res, next);