带有 NodeJS 的 PassportJS 不返回错误(MEAN 堆栈)

PassportJS with NodeJS not returning errors (MEAN stack)

我是 运行 一个使用 PassportJS 进行身份验证的 MEAN 堆栈,我的注册模块与我的 Angular 控制器交互时遇到问题。基本上,永远不会调用 errorCallback,我不确定如何正确使用 Passport done() 实现。

我有一个基本的注册表单,提交后会调用此请求:

$http.post('/api/signup', {
  name: $scope.user.name,
  email: $scope.user.email,
  password: $scope.user.password,
  userSince: new Date().now
}).then(
  function successCallback(res) {
    $rootScope.message = 'Account Created';
    console.log('Success'+res);
    console.dir(res,{depth:5});
    $location.url('/signupConf');
  }, function errorCallback(res) {
    $rootScope.message = 'Failure, see console';
    console.log('Error: '+res);
    console.dir(res,{depth:5});
    $location.url('/');
  });

快车路线:

app.post('/api/signup', passport.authenticate('local-signup'),function(req, res) {
        console.log('User: ' + req.user.email);
    });

最后是 Passport(改编自 Scotch.io tut)模块,略有删减:

passport.use('local-signup', new LocalStrategy({
    usernameField : 'email',
    passwordField : 'password',
    passReqToCallback : true 
},
function(req, email, password, done) {
  console.log("Signup Request: "+email);

  process.nextTick(function() {
    User.findOne({ 'email' : email }, function(err, user) {

      if (err) { return done(err); }

      // check to see if theres already a user with that email
      if (user) {
        console.log("User not created, already exsists: "+user);
        return done(err, false, {message: 'Username already exsists.'});
      } else {
        // if there is no user with that email
        // create the user
        var newUser = new User();
        //a bunch of data creation here

        newUser.save(function(err) {
            if (err) {throw err;}
            console.log("Sucessfully created: "+newUser);
            return done(null, newUser);
        });
      }
    });    
  });
}));

一切正常,创建的用户已更正,如果存在具有给定电子邮件的用户,则不会覆盖新用户。但是,无论如何,都会调用 successCallback。当用户名已经存在时,我可以在浏览器控制台中看到 401 错误。当它是一个错误的请求时(即不是所有的字段都被填满),一个 400 错误。

所有服务器端 console.logs 工作正常,让我认为我的 angular 前端有问题,或者后端如何响应请求。

(Scotch.io 教程学分:https://scotch.io/tutorials/easy-node-authentication-setup-and-local)

这个问题有点直面我,它在我的路线处理中。

app.post('/api/signup', function(req, res, next) {
  passport.authenticate('local-signup', function(err,user,response) {
    //handle responses based on state of user and err
  })
  (req, res, next);
});