Problem with passing Nodejs/Express parameter

Asked

Viewed 1,082 times

2

I am creating my first application in Nodejs and managed to structure the application, connection, directory structure, routes, in short, everything working right.

Now, I went to implement the login system and I’ve come across an error in the passage of the parameters to the 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 help me figure out how to pass the parameter into the passport.use?

Thanks in advance.

  • 1

    I don’t know how to answer you (because I don’t know Nodejs/Express) but I can infer from your code that at no time are you going through email and password as arguments. The parameters of your anonymous function email, password and done have nothing to do with the parameters email and password of function login. They are totally different variables. Whatever the correct way to pass them, this is certainly not it. P.S. Second that documentation if your username is not username an additional pro argument is required LocalStrategy.

  • Managed to solve?

1 answer

2

Passport also need to serialize and override the user instance,you can try using 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 the research I did: link 1 Authentication , Link2 Authentication , link3 Authentication

  • Good morning Peter, the problem is not specifically in the serialization or deserialization of the object, but in the passage of parameters, these commands are in the app.js of the application...

  • I’ll try with the nextTick()now.

  • Same problem... :(

  • try this command.. I edited..

  • I will try now Peter, thank you for the force, I have put the result.

  • postei tmb links that can help you...

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.