【问题标题】:Passport JS successRedirect hangs in Node.jsPassport JS successRedirect 在 Node.js 中挂起
【发布时间】:2018-11-15 20:01:51
【问题描述】:

我正在使用护照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
      });
    }));

【问题讨论】:

    标签: javascript node.js express passport.js


    【解决方案1】:

    试试这个方法:

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

    【讨论】:

    • 嗨,你能解释一下这个修正吗,我不确定当我的策略被称为local-signup时这会做什么@
    猜你喜欢
    • 2018-05-25
    • 1970-01-01
    • 1970-01-01
    • 2020-03-27
    • 2019-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-21
    相关资源
    最近更新 更多