Tuesday 14 March 2017

PassportJS Debugging

I have made a system to allow users to update their password via email using passport. However if the user's have NOT changed their passwords ever they cannot log in. Only user's who have changed their password's can login. I think it's because I may be mixing passports strategies somehow. If a new user try's to login (before changing their password) It crashes the app.

How would I go about debugging this? I've tried Locus Debugging and The debug module with no success. Can anyone point me in the right direction to fix this error? My set up is below:

var userSchema = new mongoose.Schema({
    username:   String,
    password:   String,
    datapoint:  String,
    email:      String,
    resetPasswordToken: String,
    resetPasswordExpires: Date
});



userSchema.pre('save', function(next) {
  var user = this;
  if (!user.isModified('password')) return next();
  bcrypt.genSalt(10, function(err, salt) {
    if (err) return next(err);
    bcrypt.hash(user.password, salt, null, function(err, hash) {
      if (err) return next(err);
      console.log('this1' + err)
      user.password = hash;
      next();
    });
  });
});

userSchema.methods.comparePassword = function(password) {
  return bcrypt.compareSync(password, this.password);
}


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



via AndrewLeonardi

No comments:

Post a Comment