Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public class AuthController {
private final UserService userService;
private final MailProducer producer;

@Operation(summary = "Login user", description = "Authenticates a user with username and password.")
@Operation(summary = "Authenticate User", description = "Authenticates a user using an account identifier and password.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "User authenticated successfully."),
@ApiResponse(responseCode = "401", description = "Invalid password.", content = @Content(mediaType = "application/json")),
@ApiResponse(responseCode = "401", description = "Invalid identifier or password.", content = @Content(mediaType = "application/json")),
@ApiResponse(responseCode = "400", description = "Missing or invalid X-Device-Id.", content = @Content(mediaType = "application/json")),
@ApiResponse(responseCode = "403", description = "Email not confirmed.", content = @Content(mediaType = "application/json")),
@ApiResponse(responseCode = "404", description = "User not found.", content = @Content(examples = {})),
Expand All @@ -49,7 +49,7 @@ public ResponseEntity<AuthRES> loginUser(
HttpServletRequest request,
@Valid @RequestBody AuthREQ dto
) {
AuthRES token = service.auth(request, dto.username(), dto.password());
AuthRES token = service.auth(request, dto.identifier(), dto.password());
return ResponseEntity.status(HttpStatus.OK).body(token);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@

import br.com.notehub.application.validation.constraints.NoForbiddenWords;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;

public record AuthREQ(
@NoForbiddenWords(message = "Não pode")
@NotBlank
@Pattern(
regexp = "^(?!.*[\\p{Zs}\\u00A0\\u2007\\u202F]).*$",
message = "Não use espaços"
)
@Size(min = 2, max = 12, message = "Tamanho inválido")
String username,
@Size(min = 2, max = 255, message = "Tamanho inválido")
String identifier,

@NotBlank
@Size(min = 4, max = 255, message = "Tamanho inválido")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,12 @@ public String validateToken(String accessToken) {

@Transactional
@Override
public AuthRES auth(HttpServletRequest request, String username, String password) throws BadCredentialsException {
public AuthRES auth(HttpServletRequest request, String identifier, String password) throws BadCredentialsException {

User user = userRepository.findByUsername(username.toLowerCase()).orElseThrow(() -> new BadCredentialsException("username"));
User user = (identifier.contains("@")
? userRepository.findByEmail(identifier)
: userRepository.findByUsername(identifier))
.orElseThrow(() -> new BadCredentialsException("identifier"));
if (!user.isActive()) throw new DisabledException("Email não confirmado");

boolean matches = encoder.matches(password, user.getPassword());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface TokenService {

String validateToken(String accessToken);

AuthRES auth(HttpServletRequest request, String username, String password) throws BadCredentialsException;
AuthRES auth(HttpServletRequest request, String identifier, String password) throws BadCredentialsException;

AuthRES authWithGoogleAcc(HttpServletRequest request, String token);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ private ResponseEntity<HttpStatus> handleEntityExistsException(EntityExistsExcep
private ResponseEntity<List<CustomResponse>> handleBadCredentialsException(BadCredentialsException ex) {
List<FieldError> errors = new ArrayList<>();
return switch (ex.getMessage()) {
case "username" -> {
errors.add(new FieldError("user", "username", "Nome não existe."));
case "identifier" -> {
errors.add(new FieldError("user", "identifier", "Identificador não existe."));
yield ResponseEntity.status(HttpStatus.NOT_FOUND).body(errors.stream().map(CustomResponse::new).toList());
}
case "password" -> {
Expand Down
Loading