For Passport-Local with Node.js, is it possible to authenticate by email and not by username?

I use passport-local to provide local authentication in my node application. However, I would like to change this for authentication with an email address and not with a username. Is there any way to do this?

Thanks.

+4
source share
3 answers

Since you must do the verification yourself (in the LocalStrategy verifier LocalStrategy ), you can pass it anything:

 passport.use(new LocalStrategy(function(email, password, done) { // search your database, or whatever, for the e-mail address // and check the password... }); 
+4
source

First, you must change the default username field to an email address using {usernameField: 'email'}, after which you can search the database based on 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 }) }) 
+12
source

You can query the database for the email id from the query parameters, as shown 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) { }); })) 
0
source

Source: https://habr.com/ru/post/1480622/


All Articles