NCBACK-9 #7
@@ -5,14 +5,17 @@ import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.validation.BeanPropertyBindingResult;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ru.soune.no_copy.dto.BaseRequest;
|
||||
import ru.soune.no_copy.dto.LoginRequest;
|
||||
import ru.soune.no_copy.dto.MessageCode;
|
||||
import ru.soune.no_copy.dto.RegRequest;
|
||||
import ru.soune.no_copy.exception.UserAlreadyExistsException;
|
||||
import ru.soune.no_copy.entity.AuthToken;
|
||||
import ru.soune.no_copy.exception.NotValidFieldException;
|
||||
import ru.soune.no_copy.handler.RegRequestValidator;
|
||||
import ru.soune.no_copy.repository.UserRepository;
|
||||
import ru.soune.no_copy.service.AuthService;
|
||||
@@ -34,6 +37,8 @@ public class ApiController {
|
||||
|
||||
private final RegRequestValidator regRequestValidator;
|
||||
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
|
||||
@Autowired
|
||||
private AuthService authService;
|
||||
|
||||
@@ -47,7 +52,7 @@ public class ApiController {
|
||||
RegRequest regRequest = objectMapper.convertValue(request.getMessageBody(), RegRequest.class);
|
||||
|
||||
if (userRepository.existsByEmail(regRequest.getEmail())) {
|
||||
throw new UserAlreadyExistsException("User already exists with email: " + regRequest.getEmail(),
|
||||
throw new NotValidFieldException("User already exists with email: " + regRequest.getEmail(),
|
||||
msgId, MessageCode.REG_EMAIL_EXISTS.getCode(), MessageCode.REG_EMAIL_EXISTS.getDescription());
|
||||
}
|
||||
|
||||
@@ -64,6 +69,20 @@ public class ApiController {
|
||||
}
|
||||
break;
|
||||
case 20001:
|
||||
LoginRequest loginRequest = objectMapper.convertValue(request.getMessageBody(), LoginRequest.class);
|
||||
|
||||
if (!userRepository.existsByEmail(loginRequest.getEmail())) {
|
||||
|
||||
throw new NotValidFieldException("User with email not found: " + loginRequest.getEmail(),
|
||||
msgId, MessageCode.AUTH_EMAIL_NOT_FOUND.getCode(),
|
||||
MessageCode.AUTH_EMAIL_NOT_FOUND.getDescription());
|
||||
}
|
||||
|
||||
authService.login(loginRequest);
|
||||
|
||||
response = fillResponseInfo(msgId, MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription());
|
||||
break;
|
||||
default:
|
||||
return ResponseEntity.ok().body(fillResponseInfo(msgId, MessageCode.MSG_ID_NOT_FOUND.getCode(),
|
||||
MessageCode.MSG_ID_NOT_FOUND.getDescription()));
|
||||
|
||||
@@ -6,17 +6,8 @@ public enum MessageCode {
|
||||
INVALID_FIELD(2002, "Invalid field"),
|
||||
MSG_ID_NOT_FOUND(2004, "Message id not found"),
|
||||
|
||||
|
||||
REGISTER_SUCCESS(10002, "Register success"),
|
||||
BAD_REQUEST(400, "Bad request"),
|
||||
UNAUTHORIZED(403, "Unauthorized"),
|
||||
FORBIDDEN(402, "Forbidden"),
|
||||
NOT_FOUND(404, "Not found"),
|
||||
INTERNAL_ERROR(500, "Internal server error"),
|
||||
|
||||
AUTH_INVALID_CREDENTIALS(101, "Invalid credentials"),
|
||||
AUTH_EMAIL_NOT_CONFIRMED(103, "Email not confirmed");
|
||||
|
||||
AUTH_EMAIL_NOT_FOUND(2005, "Email not found"),
|
||||
AUTH_PASSWORD_NOT_MATCHES(2006, "Password does not match");
|
||||
|
||||
private final Integer code;
|
||||
|
||||
|
||||
+2
-2
@@ -3,7 +3,7 @@ package ru.soune.no_copy.exception;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class UserAlreadyExistsException extends RuntimeException {
|
||||
public class NotValidFieldException extends RuntimeException {
|
||||
|
||||
private final String message;
|
||||
|
||||
@@ -13,7 +13,7 @@ public class UserAlreadyExistsException extends RuntimeException {
|
||||
|
||||
private final String messageDescription;
|
||||
|
||||
public UserAlreadyExistsException(String message, Integer messageId, Integer messageCode, String messageDescription) {
|
||||
public NotValidFieldException(String message, Integer messageId, Integer messageCode, String messageDescription) {
|
||||
super(message);
|
||||
this.message = message;
|
||||
this.messageId = messageId;
|
||||
@@ -8,10 +8,8 @@ import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import ru.soune.no_copy.dto.MessageCode;
|
||||
import ru.soune.no_copy.exception.*;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Map;
|
||||
|
||||
@RestControllerAdvice
|
||||
@@ -31,9 +29,9 @@ public class GlobalExceptionHandler {
|
||||
"message" ,message));
|
||||
}
|
||||
|
||||
@ExceptionHandler(UserAlreadyExistsException.class)
|
||||
@ExceptionHandler(NotValidFieldException.class)
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public ResponseEntity<?> handleUserContainsException(UserAlreadyExistsException ex) {
|
||||
public ResponseEntity<?> handleUserContainsException(NotValidFieldException ex) {
|
||||
|
||||
return ResponseEntity
|
||||
.ok()
|
||||
|
||||
@@ -6,12 +6,12 @@ import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.soune.no_copy.dto.LoginRequest;
|
||||
import ru.soune.no_copy.dto.MessageCode;
|
||||
import ru.soune.no_copy.dto.RegRequest;
|
||||
import ru.soune.no_copy.dto.RegisterRequest;
|
||||
import ru.soune.no_copy.entity.AuthToken;
|
||||
import ru.soune.no_copy.entity.User;
|
||||
import ru.soune.no_copy.exception.NotValidFieldException;
|
||||
import ru.soune.no_copy.exception.NotValidationPasswordException;
|
||||
import ru.soune.no_copy.exception.UserAlreadyExistsException;
|
||||
import ru.soune.no_copy.exception.UserNotFoundException;
|
||||
import ru.soune.no_copy.repository.AuthTokenRepository;
|
||||
import ru.soune.no_copy.repository.UserRepository;
|
||||
@@ -19,7 +19,6 @@ import ru.soune.no_copy.repository.UserRepository;
|
||||
import java.security.SecureRandom;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Base64;
|
||||
import java.util.Locale;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@@ -36,30 +35,6 @@ public class AuthService {
|
||||
|
||||
private final SecureRandom secureRandom = new SecureRandom();
|
||||
|
||||
// @Transactional
|
||||
// public AuthToken register(RegisterRequest registerRequest) {
|
||||
// User user = new User();
|
||||
// user.setFullName(registerRequest.fullName());
|
||||
// user.setEmail(registerRequest.email());
|
||||
// user.setPassword(passwordEncoder.encode(registerRequest.password()));
|
||||
//
|
||||
// if (registerRequest.companyName() != null) {
|
||||
// user.setCompany(registerRequest.companyName());
|
||||
// }
|
||||
//
|
||||
// if (registerRequest.phone() != null) {
|
||||
// user.setPhone(registerRequest.phone());
|
||||
// }
|
||||
//
|
||||
// User savedUser = userRepository.save(user);
|
||||
//
|
||||
// AuthToken authToken = new AuthToken();
|
||||
// authToken.setToken(generateAuthToken());
|
||||
// authToken.setUser(savedUser);
|
||||
//
|
||||
// return authTokenRepository.save(authToken);
|
||||
// }
|
||||
|
||||
@Transactional
|
||||
public AuthToken register(RegRequest registerRequest) {
|
||||
User user = new User();
|
||||
@@ -87,15 +62,12 @@ public class AuthService {
|
||||
@Transactional
|
||||
public AuthToken login(LoginRequest request) {
|
||||
Optional<User> userOpt = userRepository.findByEmail(request.getEmail());
|
||||
|
||||
if (userOpt.isEmpty()) {
|
||||
throw new UserNotFoundException("User with email " + request.getEmail() + " not found");
|
||||
}
|
||||
|
||||
User user = userOpt.get();
|
||||
|
||||
if (!passwordEncoder.matches(request.getPassword(), user.getPassword())) {
|
||||
throw new NotValidationPasswordException("Invalid password");
|
||||
throw new NotValidFieldException("Invalid password", 20003,
|
||||
MessageCode.AUTH_PASSWORD_NOT_MATCHES.getCode(),
|
||||
MessageCode.AUTH_PASSWORD_NOT_MATCHES.getDescription());
|
||||
}
|
||||
|
||||
user.setLastLoginAt(LocalDateTime.now());
|
||||
|
||||
@@ -13,7 +13,7 @@ import ru.soune.no_copy.dto.RegisterRequest;
|
||||
import ru.soune.no_copy.entity.AuthToken;
|
||||
import ru.soune.no_copy.entity.User;
|
||||
import ru.soune.no_copy.exception.NotValidationPasswordException;
|
||||
import ru.soune.no_copy.exception.UserAlreadyExistsException;
|
||||
import ru.soune.no_copy.exception.NotValidFieldException;
|
||||
import ru.soune.no_copy.exception.UserNotFoundException;
|
||||
import ru.soune.no_copy.repository.AuthTokenRepository;
|
||||
import ru.soune.no_copy.repository.UserRepository;
|
||||
@@ -80,7 +80,7 @@ public class AuthServiceTest {
|
||||
when(messageSource.getMessage(anyString(), any(), any(Locale.class)))
|
||||
.thenReturn("User exists");
|
||||
|
||||
UserAlreadyExistsException ex = assertThrows(UserAlreadyExistsException.class,
|
||||
NotValidFieldException ex = assertThrows(NotValidFieldException.class,
|
||||
() -> authService.register(request));
|
||||
|
||||
assertEquals("User exists", ex.getMessage());
|
||||
|
||||
Reference in New Issue
Block a user