Munsterberg
Munsterberg

Reputation: 828

Successful, but still using failureRedirect - passport

app.post('/signup', passport.authenticate('local-signup', {
  successRedirect: '/',
  failureRedirect: '/signup',
  failureFlash: true
}));

This is the route.

passport.use('local-signup', new LocalStrategy({
    usernameField: 'email',
    passwordField: 'password',
    passReqToCallback: true
},
function(req, email, password, done) {
    process.nextTick(function() {
        User.findOne({ 'local.email': email }, function(err, user) {
            if(err)
                return done(err);

            if(user) {
                return done(null, false, req.flash('signupMessage', 'That email is already taken'));
            } else {
                var newUser = new User();

                newUser.local.email = email;
                newUser.local.password = newUser.generateHash(password);

                newUser.save(function(err) {
                    done(err, user);
                });
            }
        });
    });
}));

This is the passport config file. The form is working as intended and being saved using my mongo schema, but it's using the failureRedirect instead of the successRedirect, anyone notice something wrong?

Upvotes: 1

Views: 246

Answers (1)

Maxim Orlov
Maxim Orlov

Reputation: 2582

When you call the save function on your newly created user you forget to pass in a 2nd argument to its callback, which is the user document returned from the db.

From your current code logic the user in your last done function is referring to the callback from findOne and therefore will never get there since if there's a user coming back from the server at that point it will execute everything in if (user) { ... }.

Make sure to pass a second argument to save like this:

newUser.save(function(err, user) {
  done(err, user);
});

Upvotes: 1

Related Questions