2

Hello i m using passport-local and i would like to make a register strategy with it.

I want the user to give username-password-firstname-lastname for registration so i tried to use passReqToCallback: true in my strategy

passport.use('local-signup', new LocalStrategy({
    passReqToCallBack: true
},
(req, username, password, done) => {
    process.nextTick(() => {
        User.findOne({ 'local.username' : username }, (err, user) => {
            if (err) return done(err);
            if (user) return done(null, false, { status: false, details: 'not ok' });
            const newUser = new User();
            newUser.local.username = username;
            newUser.local.password = newUser.generateHash(password);
            newUser.save((err) => {
                if (err) throw err;
                return done(null, newUser);
            });
        })
    });
}))

and then use req.body.firstname... in order to save it in my bd.

Here is the problem: i can t use passReqToCallback AND done function...

if you can help me with this cause everything i read tells me or to set passReqtoCallBack: true if i want to use req or to set it to false if i have done is not a function error....

Thanks

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
kikiwie
  • 390
  • 1
  • 3
  • 12
  • That's not true. Using `passReqToCallback: true`, you have both `req` and `done`. See [this](http://stackoverflow.com/questions/11784233/using-passportjs-how-does-one-pass-additional-form-fields-to-the-local-authenti). – Aᴍɪʀ Nov 21 '16 at 19:25

1 Answers1

2

you are trying to pass username and password into the callback function but you have not defined the username and password fields in the new LocalStrategy ,i.e:

  passport.use('local.signup',new LocalStrategy({
  usernameField: 'email',    **//the usernameField contains the email or the name of the user**
  passwordField: 'password',
  passReqToCallback : true  **//this line states pass request to callback is true**
}, function(req , email , password , done){   //this is callback function and it takes the req as its first argument then email , password and done as its respective arguments.
  req.checkBody('email' , 'Invalid Email').notEmpty().isEmail();
  req.checkBody('password' , 'Invalid password').notEmpty().isLength({min:4});
  var errors = req.validationErrors();
  if (errors) {
    var messages = [];
    errors.forEach(function(error){
      messages.push(error.msg);
    });
    return done(null , false , req.flash('error' , messages));
  }
  User.findOne({email: email}, function(err , user){
    if (err) {
      return done(err);
    }
    if (user) {
      return done(null , false , {message: "Email is all ready in use"});
    }
    if (!user) {
        return done(null, false, { message: 'Incorrect email' });
    }
    var newUser = new User();
    newUser.local.email = email;
    newUser.local.password = newUser.encryptPassword(password);
    newUser.save(function(err){
      if (err)
       throw err;
        return done(null, newUser);
    });
  });
}));

Hope this helps.