0.0.10+additions
This commit is contained in:
1 parent
470484064a
commit
68ab91e4fc
2 files changed
+14
-4
No files matched your search
@@ -2,8 +2,10 @@ package site.easystartup.web.auth.controller;
|
|||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.modelmapper.ModelMapper;
|
import org.modelmapper.ModelMapper;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
|
import org.springframework.security.authentication.BadCredentialsException;
|
||||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
@@ -41,6 +43,9 @@ public class AuthController {
|
|||||||
@PostMapping("/signup")
|
@PostMapping("/signup")
|
||||||
public ResponseEntity<Object> registration(@Valid @RequestBody SignupRequest signupRequest,
|
public ResponseEntity<Object> registration(@Valid @RequestBody SignupRequest signupRequest,
|
||||||
BindingResult bindingResult) {
|
BindingResult bindingResult) {
|
||||||
|
if(userService.userExists(signupRequest.getUsername())) {
|
||||||
|
return ResponseEntity.status(HttpStatus.CONFLICT).body(new MessageResponse("A user with this username already exists"));
|
||||||
|
}
|
||||||
ResponseEntity<Object> errors = responseErrorValidation.mapValidationService(bindingResult);
|
ResponseEntity<Object> errors = responseErrorValidation.mapValidationService(bindingResult);
|
||||||
if (!ObjectUtils.isEmpty(errors)) return errors;
|
if (!ObjectUtils.isEmpty(errors)) return errors;
|
||||||
|
|
||||||
@@ -54,11 +59,16 @@ public class AuthController {
|
|||||||
BindingResult bindingResult) {
|
BindingResult bindingResult) {
|
||||||
ResponseEntity<Object> errors = responseErrorValidation.mapValidationService(bindingResult);
|
ResponseEntity<Object> errors = responseErrorValidation.mapValidationService(bindingResult);
|
||||||
if (!ObjectUtils.isEmpty(errors)) return errors;
|
if (!ObjectUtils.isEmpty(errors)) return errors;
|
||||||
|
Authentication authentication;
|
||||||
Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
|
try {
|
||||||
|
authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
|
||||||
loginRequest.getUsername(),
|
loginRequest.getUsername(),
|
||||||
loginRequest.getPassword()
|
loginRequest.getPassword()
|
||||||
));
|
));
|
||||||
|
} catch (BadCredentialsException e) {
|
||||||
|
return ResponseEntity.status(401).body(new MessageResponse("Invalid login or password"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||||
|
|
||||||
|
|||||||
@@ -30,8 +30,8 @@ public class UserService {
|
|||||||
return userRepo.findById(userId).orElseThrow(() -> new UsernameNotFoundException("User not found"));
|
return userRepo.findById(userId).orElseThrow(() -> new UsernameNotFoundException("User not found"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean userExists(Long userId) {
|
public boolean userExists(String username) {
|
||||||
return userRepo.existsById(userId);
|
return userRepo.findUserByUsername(username).isPresent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user