From dee9688ece2fdf2da929ef2e988b969ebfa34bc9 Mon Sep 17 00:00:00 2001 From: Akarshit Wal Date: Sat, 7 Jan 2017 04:44:42 +0530 Subject: [PATCH] Use username also to log in (#250) * Use username also to log in * Minor changes --- client/modules/User/components/LoginForm.jsx | 4 ++-- server/config/passport.js | 8 +++++--- server/models/user.js | 11 +++++++++++ 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/client/modules/User/components/LoginForm.jsx b/client/modules/User/components/LoginForm.jsx index 037f2f81..31047940 100644 --- a/client/modules/User/components/LoginForm.jsx +++ b/client/modules/User/components/LoginForm.jsx @@ -6,10 +6,10 @@ function LoginForm(props) { return (

- + { }); /** - * Sign in using Email and Password. + * Sign in using Email/Username and Password. */ passport.use(new LocalStrategy({ usernameField: 'email' }, (email, password, done) => { - User.findOne({ email: email.toLowerCase() }, (err, user) => { // eslint-disable-line consistent-return + User.findByMailOrName(email.toLowerCase()) + .then((user) => { // eslint-disable-line consistent-return if (!user) { return done(null, false, { msg: `Email ${email} not found.` }); } @@ -28,7 +29,8 @@ passport.use(new LocalStrategy({ usernameField: 'email' }, (email, password, don } return done(null, false, { msg: 'Invalid email or password.' }); }); - }); + }) + .catch((err) => done(null, false, { msg: err })); })); /** diff --git a/server/models/user.js b/server/models/user.js index 0ac286dd..34243650 100644 --- a/server/models/user.js +++ b/server/models/user.js @@ -58,4 +58,15 @@ userSchema.methods.comparePassword = function comparePassword(candidatePassword, }); }; +userSchema.statics.findByMailOrName = function findByMailOrName(email) { + const query = { + $or: [{ + email, + }, { + username: email, + }], + }; + return this.findOne(query).exec(); +}; + export default mongoose.model('User', userSchema);