Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For Passport-Local with Node.js, is it possible to authenticate with email rather than username?

I'm using passport-local to provide local authentication on my node app. However, I would like to change this to authenticate with an email address rather than a username. Is there a way to do this?

Thank you.

like image 420
kurisukun Avatar asked Dec 04 '22 11:12

kurisukun


2 Answers

You must first change the default username field to email with { usernameField: 'email' } you can then run a database search based on the email and check the password:

passport.use(new LocalStrategy({ usernameField: 'email' }, function(email, password, done) {
  UserModel.findOne({ email: email }, function(err, user) {
    // Check password functionality  
  })
})
like image 136
Maxim McNair Avatar answered Jan 04 '23 04:01

Maxim McNair


Since you have to implement the validation yourself (in the LocalStrategy verifier callback), you can pass it anything you like:

passport.use(new LocalStrategy(function(email, password, done) {
  // search your database, or whatever, for the e-mail address
  // and check the password...
});
like image 37
robertklep Avatar answered Jan 04 '23 04:01

robertklep