Problem with NodeJS / Express parameter passing

2

I'm creating my first application in NodeJS and managed to structure the application, connection, directory structure, routes, anyway, everything working fine.

Now I've implemented the login system and I've already encountered an error in passing parameters to passport.use(new LocalStrategy(function(email,password,done)){}); .

I'm trying to call two properties passed as parameters, but it's giving error:

var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;

var userService = {

    //Se eu der um console.log() aqui, aparece os valores passados.
    login: function(email,password) {

        //O problema está aqui, quando tento passar o email e o password como parâmetro.
        passport.use(new LocalStrategy(function(email, password, done) {
            console.log(email); //Aqui não imprime mais
        });
    },
}

Could you help me figure out how to pass the parameter into passport.use ?

Thank you in advance.

    
asked by anonymous 29.09.2014 / 09:01

1 answer

2

Passport also needs to serialize and undo the user instance, you can try to use these commands below:

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

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

passport.use(new LocalStrategy(function(email, password, done) {
  User.findOne({ email: email }, {}, function(err, user) {
    if (err) { return done(err); }
    if (!user) { return done(null, false, { message: 'Unknown user ' + e }); }
    user.comparePassword(password, function(err, isMatch) {
      if (err) return done(err);
      if(isMatch) {
        return done(null, user);
      } else {
        return done(null, false, { message: 'Invalid password' });
      }
    });
  });
}));

You can also use:

passport.use(new BasicStrategy(function(email, password, done) {

Link to searches I've done: link 1 Authentication , link2 Authentication , link3 Authentication

    
29.09.2014 / 09:14