Commit cff5d984 by Jan Hrabal
parents 3900a6b3 367f05cb
...@@ -8,6 +8,7 @@ import java.util.ArrayList; ...@@ -8,6 +8,7 @@ import java.util.ArrayList;
import java.util.Base64; import java.util.Base64;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.logging.ErrorManager;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
...@@ -113,20 +114,20 @@ public class AuthApiController { ...@@ -113,20 +114,20 @@ public class AuthApiController {
throw new BadRequestException(); throw new BadRequestException();
} }
List<AuthError> errors = new ArrayList<>(); List<ErrorMessage> errors = new ArrayList<>();
//TODO validate email //TODO validate email
if (!StringUtils.hasText(signup.getLogin())) { if (!StringUtils.hasText(signup.getLogin())) {
errors.add(new AuthError("username", "BadUsername")); errors.add(ErrorMessage.withFieldCode("username", "AUTH.BAD_USERNAME"));
} }
if (!PasswordUtils.validatePassword(signup.getPassword())) { if (!PasswordUtils.validatePassword(signup.getPassword())) {
errors.add(new AuthError("password", "BadPassword")); errors.add(ErrorMessage.withFieldCode("password", "AUTH.BAD_PASSWORD"));
} }
if (!errors.isEmpty()) { if (!errors.isEmpty()) {
// throw new BadRequestException(errors); throw new RestApiException(HttpStatus.BAD_REQUEST, errors);
throw new BadRequestException(); // throw new BadRequestException();
} }
try { try {
...@@ -136,10 +137,14 @@ public class AuthApiController { ...@@ -136,10 +137,14 @@ public class AuthApiController {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
errors.add(new AuthError(null, e.getMessage())); errors.add(ErrorMessage.withMessage(e.getMessage()));
} }
return new LoginResponse("Authorization", "Basic " + Base64.getEncoder().encodeToString((signup.getLogin() + ":" + signup.getPassword()).getBytes())); if (errors.isEmpty()) {
return new LoginResponse("Authorization", "Basic " + Base64.getEncoder().encodeToString((signup.getLogin() + ":" + signup.getPassword()).getBytes()));
}
throw new RestApiException(HttpStatus.BAD_REQUEST, errors);
} }
...@@ -165,7 +170,7 @@ public class AuthApiController { ...@@ -165,7 +170,7 @@ public class AuthApiController {
return new ResponseEntity<>(Collections.singletonList(new AuthError(null, "NO_TOKEN")), HttpStatus.BAD_REQUEST); return new ResponseEntity<>(Collections.singletonList(new AuthError(null, "NO_TOKEN")), HttpStatus.BAD_REQUEST);
} }
List<AuthError> errors = new ArrayList<>(); List<ErrorMessage> errors = new ArrayList<>();
try { try {
authService.resetPassword(resetPassword.getLogin(), resetPassword.getToken(), resetPassword.getPassword()); authService.resetPassword(resetPassword.getLogin(), resetPassword.getToken(), resetPassword.getPassword());
...@@ -177,7 +182,7 @@ public class AuthApiController { ...@@ -177,7 +182,7 @@ public class AuthApiController {
// } // }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
errors.add(new AuthError(null, e.getMessage())); errors.add(ErrorMessage.withMessage(e.getMessage()));
} }
if (errors.isEmpty()) { if (errors.isEmpty()) {
...@@ -185,7 +190,7 @@ public class AuthApiController { ...@@ -185,7 +190,7 @@ public class AuthApiController {
return new ResponseEntity<>(HttpStatus.NO_CONTENT); return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} }
return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST); throw new RestApiException(HttpStatus.BAD_REQUEST, errors);
} }
......
...@@ -110,7 +110,7 @@ public class ErrorMessage { ...@@ -110,7 +110,7 @@ public class ErrorMessage {
*/ */
public static ErrorMessage withCode(String code) { public static ErrorMessage withCode(String code) {
ErrorMessage em = new ErrorMessage(); ErrorMessage em = new ErrorMessage();
em.message = code; em.code = code;
return em; return em;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment