public async Task <AuthenticatedUser> Handle(LoginData message, CancellationToken cancellationToken) { LoginDataValidator validator = new LoginDataValidator(); var results = validator.Validate(message); // null or empty fields if (results.IsValid == false) { throw new RestException(HttpStatusCode.BadRequest, results.Errors); } User user = await _context.Users.Where(u => u.EmailAddress == message.EmailAddress).SingleOrDefaultAsync(); // not found if (user == null) { throw new RestException(HttpStatusCode.NotFound, "User with email address not found"); } // wrong password if (!user.Password.Equals(message.Password)) { throw new RestException(HttpStatusCode.Forbidden, "Password or email does not match"); } return(new AuthenticatedUser { Token = await _jwtTokenGenerator.CreateToken(user.EmailAddress), Username = user.Username }); }
public AuthController( ILoginService loginService, IRegistrationService registrationService) { this.loginService = loginService; this.registrationService = registrationService; loginDataValidator = new LoginDataValidator(); registrationDataValidator = new RegistrationDataValidator(); }