Home Ask Login Register

Developers Planet

Your answer is one click away!

Lucas February 2016

PassportJS with NodeJS not returning errors (MEAN stack)

I'm running a MEAN stack with PassportJS for authentication, and I'm having an issue with my signup module interacting with my Angular controller. Basically, the errorCallback is never called, and I'm not sure how to properly use the Passport done() implementation.

I have a basic signup form that upon submission, calls this request:

$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('/');
  });

With the express route:

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

And finally the Passport (adapted from a Scotch.io tut) module, abridged a little:

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;}
                

Answers


Lucas February 2016

The problem was sort of staring me in the face, it was in my route handling.

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);
});

Post Status

Asked in February 2016
Viewed 3,704 times
Voted 8
Answered 1 times

Search




Leave an answer


Quote of the day: live life