Skip to content

Commit

Permalink
hotfix: improve code readability and update error handling for user e…
Browse files Browse the repository at this point in the history
…xistence checks and email user signup notification
  • Loading branch information
LeonardoMeireles55 committed Feb 6, 2025
1 parent 341e34f commit 0f41e94
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ private void sendRecoveryEmail(RecoveryEmailDTO recoveryEmailDTO) {
Best regards,
Your Team""", recoveryEmailDTO.temporaryPassword());
log.info("Sending recovery identifier to: {}", recoveryEmailDTO.email());
this.emailService.sendPlainTextEmail(new EmailDTO(recoveryEmailDTO.email(), subject, message));
this.emailService
.sendPlainTextEmail(new EmailDTO(recoveryEmailDTO.email(), subject, message));
}

public void recoverPassword(String username, String email) {
Expand All @@ -61,7 +62,8 @@ public void changePassword(String email, String temporaryPassword, String newPas
if (!this.passwordRecoveryTokenManager.isRecoveryTokenValid(temporaryPassword, email)) {
throw new CustomGlobalErrorHandling.RecoveryTokenInvalidException();
}
this.userRepository.setPasswordWhereByEmail(email, BCryptEncoderComponent.encrypt(newPassword));
this.userRepository.setPasswordWhereByEmail(email,
BCryptEncoderComponent.encrypt(newPassword));
}

private TokenJwtDTO authenticateAndGenerateToken(User credential, String password) {
Expand Down Expand Up @@ -94,19 +96,23 @@ public User signUp(String username, String email, String password) {

var user =
new User(username, BCryptEncoderComponent.encrypt(password), email, UserRoles.USER);
var savedUser = this.userRepository.save(user);

try {
this.emailService.notifyUserSignup(user.getUsername(), user.getEmail(), LocalDateTime.now());
this.emailService.notifyUserSignup(savedUser.getUsername(), savedUser.getEmail(),
LocalDateTime.now());
} catch (Exception e) {
log.error("Failed signup for user: {}. Exception: ", user.getEmail(), e);
log.error("Failed to send signup notification for user: {}. Exception: ",
savedUser.getEmail(), e);
}

return this.userRepository.save(user);
return savedUser;
}

public TokenJwtDTO signIn(String identifier, String password) {
try {
final var credential = this.userRepository.findOneByUsernameOrEmail(identifier, identifier);
final var credential =
this.userRepository.findOneByUsernameOrEmail(identifier, identifier);

if (credential == null) {
throw new CustomGlobalErrorHandling.UserNotFoundException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ public ResponseEntity<ApiError> handleAccessDenied(HttpServletRequest request) {
@ResponseStatus(HttpStatus.CONFLICT)
public ResponseEntity<ApiError> handleUserAlreadyExist(UserAlreadyExistException ex,
HttpServletRequest request) {
ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST,
"Username or identifier already exists", request.getRequestURI());
ApiError apiError = new ApiError(HttpStatus.CONFLICT,
"A user with this username or email already exists", request.getRequestURI());

log.error("Username or identifier already exists at {}: {}", request.getRequestURI(),
ex.getMessage());
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(apiError);
log.error("A user with this username or email already exists at {}: {}",
request.getRequestURI(), ex.getMessage());
return ResponseEntity.status(HttpStatus.CONFLICT).body(apiError);
}

@ExceptionHandler(DataIntegrityViolationException.class)
Expand Down

0 comments on commit 0f41e94

Please sign in to comment.