Andre Coetzee
Andre Coetzee

Reputation: 1310

PassportJS just hangs after creating a user

After I signup a user using passport the screen just hangs and it does not redirect me to the screen set in successRedirect:

model/user.js

var bcrypt = require('bcrypt');

module.exports = function(sequelize, DataType){

  var User = sequelize.define('User', {
    name: DataType.STRING,
    localPassword: DataType.STRING,
    lastName: DataType.STRING,
    localEmail: DataType.STRING,
    cellNumber: DataType.INTEGER
  },
      {
        instanceMethods: {
          validPassword: function(password){
              return bcrypt.compareSync(password, this.password);
          }
        }
      }
  );
    return User;
};

routes/index.js

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

config/passportConfig.js

passport.serializeUser(function(user, done) {
    done(null, user.id);
});

passport.deserializeUser(function(id, done) {
    model.User.findById(id, function(err, user){
        done(null, user);
    });
});


passport.use('local-signup', new LocalStrategy({
        usernameField : 'email',
        passwordField : 'password',
        passReqToCallback : true
    },
    function(req, email, password, done) {
        model.User.findOne({
            where: {
                localEmail: email
            }
        }).then(function(existingUser) {
                if (existingUser) {
                    return done(null, false, req.flash('loginMessage', 'That email is already taken.'));
                }
                else {
                    var hash = bcrypt.hashSync(password, 8);
                    var newUser = model.User.build({name: req.body.name, lastName: req.body.lastName, localEmail: req.body.email, cellNumber: req.body.cellNumber, localPassword: hash});
                    newUser.save().then(function(){
                        done(null, newUser);
                    }).catch(function(err){
                        done(null, false, req.flash('loginMessage', err))
                    });
                }
            })
            .catch(function (e) {
                done(null, false, req.flash('loginMessage',e.name + " " + e.message));
            })
    }));

so basically what happens is it creates the entry in the database and the it stays on the register screen and loads. It says waiting for localhost, but goes on forever. I am new to passport and sequelize, any help would be greatly appreciated.

Upvotes: 1

Views: 147

Answers (1)

Ben Polge
Ben Polge

Reputation: 646

I believe that your problem lies in this code block:

newUser.save().then(function(){
   done(null, newUser);
})

Since you are calling done but not returning it, Bluebird (the promise lib that sequelize uses) sees that promise as returning undefined. Therefor it runs the rest of the code in parallel without waiting for it to resolve.

Try adding a return to the statement:

newUser.save().then(function(){
    return done(null, newUser);
})

Upvotes: 1

Related Questions