public async Task <object> AuthenticationUser(FormLoginDTO userDTO) { try { var result = await userRepository.FindByEmail(userDTO.email); if (result != null) { if (passwordHelper.VerifyPasswordHash(result.Id, userDTO.password, result.Password)) { return(new LoginDTO(jwtHelper.GenerateJWT(result.Name == "" || result.Name == null ? result.Email : result.Name, result.Role), true)); } else { return(new { success = false, message = "Oops, e-mail ou senha incorreto." }); } } else { return(new { success = false, message = "Oops, e-mail ou senha incorreto." }); } }catch (Exception ex) { return(new { success = false, message = "Oops, ocorreu um erro no servidor, tente novamente mais tarde." }); } }
public AuthenticateUserResponse Authenticate(string emailAddress, string password) { AuthenticateUserResponse response = new AuthenticateUserResponse(); _logger.LogInformation($"Authenticating user with email: {emailAddress}"); if (string.IsNullOrEmpty(emailAddress) || string.IsNullOrEmpty(password)) { response.ResponseStatus.SetError(ResponseStatusCode.BAD_REQUEST, Constants.AuthenticateUserMessages.InvalidCredentials); return(response); } User user = null; try { user = _userRepository.GetByEmailAddress(emailAddress); } catch (Exception e) { _logger.LogError(e.ToString()); response.ResponseStatus.SetError(ResponseStatusCode.INTERNAL_SERVER_ERROR, e.ToString()); return(response); } if (user is null) { response.ResponseStatus.SetError(ResponseStatusCode.UNAUTHORIZED, Constants.AuthenticateUserMessages.InvalidCredentials); return(response); } try { bool passwordIsCorrect = _authenticationHelper.VerifyPasswordHash(password, user.PasswordHash, user.PasswordSalt); if (!passwordIsCorrect) { response.ResponseStatus.SetError(ResponseStatusCode.UNAUTHORIZED, Constants.AuthenticateUserMessages.InvalidCredentials); return(response); } } catch (Exception e) { _logger.LogError(e.ToString()); response.ResponseStatus.SetError(ResponseStatusCode.INTERNAL_SERVER_ERROR, e.ToString()); return(response); } string tokenString = _jwtHelper.GenerateJWT(user.UserId, ConfigurationsHelper.JWTSecret); if (string.IsNullOrEmpty(tokenString)) { response.ResponseStatus.SetError(ResponseStatusCode.INTERNAL_SERVER_ERROR, Constants.AuthenticateUserMessages.FailedToGenerateJWT); return(response); } _logger.LogInformation("Successfully authenticated user"); UserDto userDto = _mapper.Map <UserDto>(user); response.ResponseStatus.SetOk(); response.UserDto = userDto; response.Token = tokenString; return(response); }