implement registration validity checks
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-08-09 04:56:54 +02:00
parent c0f655b0df
commit 17dcc0ac4f
9 changed files with 108 additions and 27 deletions

View File

@@ -1,12 +1,17 @@
package wtf.beatrice.releasehive.services;
import org.apache.coyote.BadRequestException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import wtf.beatrice.releasehive.config.InternalConfiguration;
import wtf.beatrice.releasehive.dtos.LoginUserDto;
import wtf.beatrice.releasehive.dtos.RegisterUserDto;
import wtf.beatrice.releasehive.exceptions.HiveException;
import wtf.beatrice.releasehive.models.User;
import wtf.beatrice.releasehive.repositories.UserRepository;
@@ -28,7 +33,37 @@ public class AccountServiceImpl implements AccountService {
}
@Override
public User register(RegisterUserDto userDto) {
public User register(RegisterUserDto userDto) throws BadRequestException {
if (userDto.getEmail() == null ||
userDto.getEmail().isEmpty() ||
userDto.getPassword() == null ||
userDto.getPassword().isEmpty() ||
userDto.getUsername() == null ||
userDto.getUsername().isEmpty()) {
throw new BadRequestException("Please provide a valid email, password, and username");
}
if(!userDto.getEmail().matches(InternalConfiguration.EMAIL_REGEX_RCF)) {
throw new BadRequestException("Invalid email format");
}
if(!userDto.getUsername().matches(InternalConfiguration.USERNAME_REGEX)) {
throw new BadRequestException("Username contains invalid characters");
}
if(!userDto.getPassword().matches(InternalConfiguration.PASSWORD_REGEX)) {
throw new BadRequestException("Invalid password format");
}
if (userRepository.findByEmail(userDto.getEmail()).isPresent()) {
throw new BadRequestException("An account already exists with this email");
}
if(userRepository.findByUsername(userDto.getUsername()).isPresent()) {
throw new BadRequestException("Username already in use");
}
User user = new User();
user.setUsername(userDto.getUsername());