Skip to content
Snippets Groups Projects
Commit 80bf7c58 authored by Karsch Lukas's avatar Karsch Lukas
Browse files

#45 - fixed API status codes in auth controller (swagger)

parent cacdb033
No related branches found
No related tags found
1 merge request!44resolve #45 - error status code added
......@@ -11,6 +11,5 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor
public class AuthenticationResponse {
private String token;
private String error;
private String message;
}
package hdm.mi.growbros.auth;
import hdm.mi.growbros.exceptions.*;
import hdm.mi.growbros.exceptions.EmailAlreadyExistsException;
import hdm.mi.growbros.exceptions.InvalidDataException;
import hdm.mi.growbros.exceptions.InvalidEmailException;
import hdm.mi.growbros.exceptions.UserNotFoundException;
import hdm.mi.growbros.models.user.Role;
import hdm.mi.growbros.models.user.User;
import hdm.mi.growbros.repositories.UserRepository;
import hdm.mi.growbros.security.JwtService;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
......@@ -60,7 +61,7 @@ public class AuthenticationService {
.token(jwtToken)
.build();
} catch (DataIntegrityViolationException e) {
throw new EmailAlreadyExistsException(HttpStatus.BAD_REQUEST, "Email ist bereits registriert.");
throw new EmailAlreadyExistsException("Email ist bereits registriert.");
}
}
......
......@@ -4,11 +4,10 @@ import hdm.mi.growbros.auth.AuthenticationRequest;
import hdm.mi.growbros.auth.AuthenticationResponse;
import hdm.mi.growbros.auth.AuthenticationService;
import hdm.mi.growbros.auth.RegisterRequest;
import hdm.mi.growbros.exceptions.EmailAlreadyExistsException;
import hdm.mi.growbros.exceptions.GrowBrosException;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
......@@ -21,44 +20,32 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/api/v1/auth")
@RequiredArgsConstructor
public class AuthenticationController {
private final AuthenticationService authenticationService;
@PostMapping("/register")
@ApiOperation(value = "Register user", response = AuthenticationResponse.class)
@Operation(description = "Register a new user")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successfully registered", response = AuthenticationResponse.class),
@ApiResponse(code = 409, message = "Email already exists"),
@ApiResponse(code = 500, message = "Internal Server Error")
@ApiResponse(responseCode = "200", description = "Successfully registered"),
@ApiResponse(responseCode = "400", description = "Invalid data", content = @Content(mediaType = "text/plain")),
@ApiResponse(responseCode = "409", description = "Email already exists", content = @Content(mediaType = "text/plain"))
})
public ResponseEntity<AuthenticationResponse> register(
@RequestBody RegisterRequest request
) {
try {
AuthenticationResponse response = authenticationService.register(request);
return new ResponseEntity<>(response, HttpStatus.OK);
} catch (EmailAlreadyExistsException e) {
return new ResponseEntity<>(HttpStatus.CONFLICT);
} catch (GrowBrosException e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
AuthenticationResponse response = authenticationService.register(request);
return new ResponseEntity<>(response, HttpStatus.OK);
}
@PostMapping("/authenticate")
@ApiOperation(value = "Authenticate user", response = AuthenticationResponse.class)
@Operation(description = "Authenticate user")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successfully authenticated", response = AuthenticationResponse.class),
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 500, message = "Internal Server Error")
@ApiResponse(responseCode = "200", description = "Successfully authenticated"),
@ApiResponse(responseCode = "401", description = "Unauthorized (wrong credentials)", content = @Content(mediaType = "text/plain"))
})
public ResponseEntity<AuthenticationResponse> authenticate(
@RequestBody AuthenticationRequest request
) {
try {
AuthenticationResponse response = authenticationService.authenticate(request);
return new ResponseEntity<>(response, HttpStatus.OK);
} catch (GrowBrosException e) {
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
AuthenticationResponse response = authenticationService.authenticate(request);
return new ResponseEntity<>(response, HttpStatus.OK);
}
}
......@@ -25,6 +25,7 @@ public class RestErrorHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(AccessDeniedException.class)
protected ResponseEntity<String> handleAccessDeniedException() {
log.warn("handling access denied exception");
return ResponseEntity.status(401).body("Access denied: role not sufficient");
}
......
......@@ -2,8 +2,8 @@ package hdm.mi.growbros.exceptions;
import org.springframework.http.HttpStatus;
public class EmailAlreadyExistsException extends GrowBrosException{
public EmailAlreadyExistsException(HttpStatus httpStatus, String message) {
super(HttpStatus.BAD_REQUEST, message);
public class EmailAlreadyExistsException extends GrowBrosException {
public EmailAlreadyExistsException(String message) {
super(HttpStatus.CONFLICT, message);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment