From 68ab91e4fcbb40b781b1e9ba728b3226a2a58744 Mon Sep 17 00:00:00 2001 From: iRedTea Date: Tue, 27 Jun 2023 14:22:46 +0300 Subject: [PATCH] 0.0.10+additions --- .../web/auth/controller/AuthController.java | 18 ++++++++++++++---- .../easystartup/web/service/UserService.java | 4 ++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main/java/site/easystartup/web/auth/controller/AuthController.java b/src/main/java/site/easystartup/web/auth/controller/AuthController.java index d3f9d5c..0c55df8 100644 --- a/src/main/java/site/easystartup/web/auth/controller/AuthController.java +++ b/src/main/java/site/easystartup/web/auth/controller/AuthController.java @@ -2,8 +2,10 @@ package site.easystartup.web.auth.controller; import lombok.RequiredArgsConstructor; import org.modelmapper.ModelMapper; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; @@ -41,6 +43,9 @@ public class AuthController { @PostMapping("/signup") public ResponseEntity registration(@Valid @RequestBody SignupRequest signupRequest, BindingResult bindingResult) { + if(userService.userExists(signupRequest.getUsername())) { + return ResponseEntity.status(HttpStatus.CONFLICT).body(new MessageResponse("A user with this username already exists")); + } ResponseEntity errors = responseErrorValidation.mapValidationService(bindingResult); if (!ObjectUtils.isEmpty(errors)) return errors; @@ -54,11 +59,16 @@ public class AuthController { BindingResult bindingResult) { ResponseEntity errors = responseErrorValidation.mapValidationService(bindingResult); if (!ObjectUtils.isEmpty(errors)) return errors; + Authentication authentication; + try { + authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken( + loginRequest.getUsername(), + loginRequest.getPassword() + )); + } catch (BadCredentialsException e) { + return ResponseEntity.status(401).body(new MessageResponse("Invalid login or password")); + } - Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken( - loginRequest.getUsername(), - loginRequest.getPassword() - )); SecurityContextHolder.getContext().setAuthentication(authentication); diff --git a/src/main/java/site/easystartup/web/service/UserService.java b/src/main/java/site/easystartup/web/service/UserService.java index eba8354..f04aa11 100644 --- a/src/main/java/site/easystartup/web/service/UserService.java +++ b/src/main/java/site/easystartup/web/service/UserService.java @@ -30,8 +30,8 @@ public class UserService { return userRepo.findById(userId).orElseThrow(() -> new UsernameNotFoundException("User not found")); } - public boolean userExists(Long userId) { - return userRepo.existsById(userId); + public boolean userExists(String username) { + return userRepo.findUserByUsername(username).isPresent(); }