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