Node.js Logging into your passport strategy using email or username

I am using Passport for my node.js application, and currently I am using a username to login.

On my user registration page, I allow the user to register their unique username and email address.

I need a login page using "Login using username / email:" ________

Where the script can determine if there is an "@" in the field and look for an email address instead of a username.

I tried several hours to no avail.

here is my passport .js

var mongoose = require('mongoose') var LocalStrategy = require('passport-local').Strategy var User = mongoose.model('User'); module.exports = function(passport, config){ passport.serializeUser(function(user, done){ done(null, user.id); }) passport.deserializeUser(function(id, done) { User.findOne({ _id: id }, function (err, user) { done(err, user); }); }); passport.use(new LocalStrategy({ usernameField: 'username', passwordField: 'password' }, function(username, password, done) { User.isValidUserPassword(username, password, done); })); } 

EDIT: below user user.js on request

 var mongoose = require('mongoose'); var hash = require('../util/hash.js'); UserSchema = mongoose.Schema({ username: String, email: String, salt: String, hash: String }) UserSchema.statics.signup = function(username, email, password, done){ var User = this; hash(password, function(err, salt, hash){ if(err) throw err; // if (err) return done(err); User.create({ username : username, email: email, salt : salt, hash : hash }, function(err, user){ if(err) throw err; // if (err) return done(err); done(null, user); }); }); } UserSchema.statics.isValidUserPassword = function(username, password, done) { this.findOne({username : username}, function(err, user){ // if(err) throw err; if(err) return done(err); if(!user) return done(null, false, { message : 'Incorrect username.' }); hash(password, user.salt, function(err, hash){ if(err) return done(err); if(hash == user.hash) return done(null, user); done(null, false, { message : 'Incorrect password' }); }); }); }; var User = mongoose.model("User", UserSchema); module.exports = User; 
+7
source share
4 answers

Ok, you should have something like this in your Mongoose model:

 UserSchema.statics.isValidUserPassword = function(username, password, done) { var criteria = (username.indexOf('@') === -1) ? {username: username} : {email: username}; this.findOne(criteria, function(err, user){ // All the same... }); }; 
+23
source

its a mongoose thing, not a passport thing, and if you can try it using the answer bredikhin ^ _ ^:

 var criteria = {$or: [{username: username}, {email: username}, {mobile: username}, {anything: username}]}; 

The key is to find the user through a request from mongodb !!!
Thus, you can have any query that you want to find.

+5
source

To enter a user name or e-mail, you can also use the passport-local-mongoose findByUsername and change the setting queryParameters.

 // add passport functions // schema.plugin(passportLocalMongoose); schema.plugin(passportLocalMongoose, { // allow sign in with username OR email findByUsername: function(model, queryParameters) { // start // // queryParameters => { '$or' : [ { 'username' : 'searchString' } ] } // iterate through queryParameters for( let param of queryParameters.$or ){ // if there is a username if( typeof param == "object" && param.hasOwnProperty("username") ){ // add it as an email parameter queryParameters.$or.push( { email : param.username } ); } } // expected outcome // queryParameters => { '$or' : [ { 'username' : 'searchString' }, { 'email' : 'searchString' } ] } return model.findOne(queryParameters); } }); 
0
source

it is very simple, you need to redefine the passport strategy, as in the code below,

serialize with username

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

deserialize with username

 passport.deserializeUser(function(username, done) { User.findOne({username:username},function(err, user){ done(err,user); }); }); 

Passport strategy

 //passport strategy passport.use(new LocalStrategy(function(username, password, done) { console.log(username.includes("@")); User.findOne((username.includes("@"))?{email:username}:{username:username}, function(err, user) { if (err) {return done(err); } if (!user) {console.log("i am ERROR"); return done(null, false, { message: 'Incorrect username.' });} if (user.password===password) {return done(null, user); } return done(null, false); }); } )); 

NOTE. Here user.password===password means that the password in the database is stored in clear text. And you need to manually enter the password into the database, for example password: req.body.password you also need to apply encryption and decryption yourself. before adding or in comparison.

0
source

All Articles