@@ -5,14 +5,17 @@ import lombok.RequiredArgsConstructor;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.validation.BeanPropertyBindingResult;
|
import org.springframework.validation.BeanPropertyBindingResult;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.validation.FieldError;
|
import org.springframework.validation.FieldError;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import ru.soune.no_copy.dto.BaseRequest;
|
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.MessageCode;
|
||||||
import ru.soune.no_copy.dto.RegRequest;
|
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.handler.RegRequestValidator;
|
||||||
import ru.soune.no_copy.repository.UserRepository;
|
import ru.soune.no_copy.repository.UserRepository;
|
||||||
import ru.soune.no_copy.service.AuthService;
|
import ru.soune.no_copy.service.AuthService;
|
||||||
@@ -34,6 +37,8 @@ public class ApiController {
|
|||||||
|
|
||||||
private final RegRequestValidator regRequestValidator;
|
private final RegRequestValidator regRequestValidator;
|
||||||
|
|
||||||
|
private final PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private AuthService authService;
|
private AuthService authService;
|
||||||
|
|
||||||
@@ -47,7 +52,7 @@ public class ApiController {
|
|||||||
RegRequest regRequest = objectMapper.convertValue(request.getMessageBody(), RegRequest.class);
|
RegRequest regRequest = objectMapper.convertValue(request.getMessageBody(), RegRequest.class);
|
||||||
|
|
||||||
if (userRepository.existsByEmail(regRequest.getEmail())) {
|
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());
|
msgId, MessageCode.REG_EMAIL_EXISTS.getCode(), MessageCode.REG_EMAIL_EXISTS.getDescription());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,6 +69,20 @@ public class ApiController {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 20001:
|
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:
|
default:
|
||||||
return ResponseEntity.ok().body(fillResponseInfo(msgId, MessageCode.MSG_ID_NOT_FOUND.getCode(),
|
return ResponseEntity.ok().body(fillResponseInfo(msgId, MessageCode.MSG_ID_NOT_FOUND.getCode(),
|
||||||
MessageCode.MSG_ID_NOT_FOUND.getDescription()));
|
MessageCode.MSG_ID_NOT_FOUND.getDescription()));
|
||||||
|
|||||||
@@ -6,17 +6,8 @@ public enum MessageCode {
|
|||||||
INVALID_FIELD(2002, "Invalid field"),
|
INVALID_FIELD(2002, "Invalid field"),
|
||||||
MSG_ID_NOT_FOUND(2004, "Message id not found"),
|
MSG_ID_NOT_FOUND(2004, "Message id not found"),
|
||||||
|
|
||||||
|
AUTH_EMAIL_NOT_FOUND(2005, "Email not found"),
|
||||||
REGISTER_SUCCESS(10002, "Register success"),
|
AUTH_PASSWORD_NOT_MATCHES(2006, "Password does not match");
|
||||||
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");
|
|
||||||
|
|
||||||
|
|
||||||
private final Integer code;
|
private final Integer code;
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -3,7 +3,7 @@ package ru.soune.no_copy.exception;
|
|||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
public class UserAlreadyExistsException extends RuntimeException {
|
public class NotValidFieldException extends RuntimeException {
|
||||||
|
|
||||||
private final String message;
|
private final String message;
|
||||||
|
|
||||||
@@ -13,7 +13,7 @@ public class UserAlreadyExistsException extends RuntimeException {
|
|||||||
|
|
||||||
private final String messageDescription;
|
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);
|
super(message);
|
||||||
this.message = message;
|
this.message = message;
|
||||||
this.messageId = messageId;
|
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.ExceptionHandler;
|
||||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
import ru.soune.no_copy.dto.MessageCode;
|
|
||||||
import ru.soune.no_copy.exception.*;
|
import ru.soune.no_copy.exception.*;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@RestControllerAdvice
|
@RestControllerAdvice
|
||||||
@@ -31,9 +29,9 @@ public class GlobalExceptionHandler {
|
|||||||
"message" ,message));
|
"message" ,message));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(UserAlreadyExistsException.class)
|
@ExceptionHandler(NotValidFieldException.class)
|
||||||
@ResponseStatus(HttpStatus.OK)
|
@ResponseStatus(HttpStatus.OK)
|
||||||
public ResponseEntity<?> handleUserContainsException(UserAlreadyExistsException ex) {
|
public ResponseEntity<?> handleUserContainsException(NotValidFieldException ex) {
|
||||||
|
|
||||||
return ResponseEntity
|
return ResponseEntity
|
||||||
.ok()
|
.ok()
|
||||||
|
|||||||
@@ -6,12 +6,12 @@ import org.springframework.security.crypto.password.PasswordEncoder;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import ru.soune.no_copy.dto.LoginRequest;
|
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.RegRequest;
|
||||||
import ru.soune.no_copy.dto.RegisterRequest;
|
|
||||||
import ru.soune.no_copy.entity.AuthToken;
|
import ru.soune.no_copy.entity.AuthToken;
|
||||||
import ru.soune.no_copy.entity.User;
|
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.NotValidationPasswordException;
|
||||||
import ru.soune.no_copy.exception.UserAlreadyExistsException;
|
|
||||||
import ru.soune.no_copy.exception.UserNotFoundException;
|
import ru.soune.no_copy.exception.UserNotFoundException;
|
||||||
import ru.soune.no_copy.repository.AuthTokenRepository;
|
import ru.soune.no_copy.repository.AuthTokenRepository;
|
||||||
import ru.soune.no_copy.repository.UserRepository;
|
import ru.soune.no_copy.repository.UserRepository;
|
||||||
@@ -19,7 +19,6 @@ import ru.soune.no_copy.repository.UserRepository;
|
|||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.Locale;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@@ -36,30 +35,6 @@ public class AuthService {
|
|||||||
|
|
||||||
private final SecureRandom secureRandom = new SecureRandom();
|
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
|
@Transactional
|
||||||
public AuthToken register(RegRequest registerRequest) {
|
public AuthToken register(RegRequest registerRequest) {
|
||||||
User user = new User();
|
User user = new User();
|
||||||
@@ -87,15 +62,12 @@ public class AuthService {
|
|||||||
@Transactional
|
@Transactional
|
||||||
public AuthToken login(LoginRequest request) {
|
public AuthToken login(LoginRequest request) {
|
||||||
Optional<User> userOpt = userRepository.findByEmail(request.getEmail());
|
Optional<User> userOpt = userRepository.findByEmail(request.getEmail());
|
||||||
|
|
||||||
if (userOpt.isEmpty()) {
|
|
||||||
throw new UserNotFoundException("User with email " + request.getEmail() + " not found");
|
|
||||||
}
|
|
||||||
|
|
||||||
User user = userOpt.get();
|
User user = userOpt.get();
|
||||||
|
|
||||||
if (!passwordEncoder.matches(request.getPassword(), user.getPassword())) {
|
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());
|
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.AuthToken;
|
||||||
import ru.soune.no_copy.entity.User;
|
import ru.soune.no_copy.entity.User;
|
||||||
import ru.soune.no_copy.exception.NotValidationPasswordException;
|
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.exception.UserNotFoundException;
|
||||||
import ru.soune.no_copy.repository.AuthTokenRepository;
|
import ru.soune.no_copy.repository.AuthTokenRepository;
|
||||||
import ru.soune.no_copy.repository.UserRepository;
|
import ru.soune.no_copy.repository.UserRepository;
|
||||||
@@ -80,7 +80,7 @@ public class AuthServiceTest {
|
|||||||
when(messageSource.getMessage(anyString(), any(), any(Locale.class)))
|
when(messageSource.getMessage(anyString(), any(), any(Locale.class)))
|
||||||
.thenReturn("User exists");
|
.thenReturn("User exists");
|
||||||
|
|
||||||
UserAlreadyExistsException ex = assertThrows(UserAlreadyExistsException.class,
|
NotValidFieldException ex = assertThrows(NotValidFieldException.class,
|
||||||
() -> authService.register(request));
|
() -> authService.register(request));
|
||||||
|
|
||||||
assertEquals("User exists", ex.getMessage());
|
assertEquals("User exists", ex.getMessage());
|
||||||
|
|||||||
Reference in New Issue
Block a user