Use username also to log in (#250)
* Use username also to log in * Minor changes
This commit is contained in:
		
							parent
							
								
									c6ecda4f3a
								
							
						
					
					
						commit
						dee9688ece
					
				
					 3 changed files with 18 additions and 5 deletions
				
			
		| 
						 | 
				
			
			@ -6,10 +6,10 @@ function LoginForm(props) {
 | 
			
		|||
  return (
 | 
			
		||||
    <form className="form" onSubmit={handleSubmit(props.validateAndLoginUser.bind(this, props.previousPath))}>
 | 
			
		||||
      <p className="form__field">
 | 
			
		||||
        <label htmlFor="email" className="form__label">Email</label>
 | 
			
		||||
        <label htmlFor="email" className="form__label">Email or Username</label>
 | 
			
		||||
        <input
 | 
			
		||||
          className="form__input"
 | 
			
		||||
          aria-label="email"
 | 
			
		||||
          aria-label="email or username"
 | 
			
		||||
          type="text"
 | 
			
		||||
          id="email"
 | 
			
		||||
          {...domOnlyProps(email)}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -15,10 +15,11 @@ passport.deserializeUser((id, done) => {
 | 
			
		|||
});
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 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 }));
 | 
			
		||||
}));
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in a new issue