kurisukun
kurisukun

Reputation: 3159

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.

Upvotes: 4

Views: 2037

Answers (3)

Jyoti Duhan
Jyoti Duhan

Reputation: 1094

You can query the database for the email-id from request params like below:

passport.use('signup', new LocalStrategy({
        passReqToCallback : true // allows us to pass back the entire request to the callback
    },
    function(req, username, password, done) {
        //console.log(email);
        console.log(req.param('email'));
        findOrCreateUser = function(){
            // find a user in Mongo with provided username
            User.findOne({ 'email' :  req.param('email') }, function(err, user) {

            });
        }))    

Upvotes: 0

Maxim McNair
Maxim McNair

Reputation: 1018

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  
  })
})

Upvotes: 11

robertklep
robertklep

Reputation: 203304

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...
});

Upvotes: 4

Related Questions