add checks for login process
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
86f4b05b8c
commit
d656ab54eb
@ -39,9 +39,8 @@ public class AuthResource {
|
||||
@PostMapping(
|
||||
value="/login",
|
||||
produces="application/json")
|
||||
public ResponseEntity<LoginResponse> login(@RequestBody LoginUserDto userDto)
|
||||
{
|
||||
User authenticatedUser = accountService.login(userDto);
|
||||
public ResponseEntity<LoginResponse> login(@RequestBody LoginUserDto userDto) throws BadRequestException {
|
||||
User authenticatedUser = accountService.authenticate(userDto);
|
||||
|
||||
String jwtToken = jwtService.generateToken(authenticatedUser);
|
||||
|
||||
|
@ -10,5 +10,5 @@ public interface AccountService
|
||||
|
||||
User register(RegisterUserDto user) throws BadRequestException;
|
||||
|
||||
User login(LoginUserDto user);
|
||||
User authenticate(LoginUserDto user) throws BadRequestException;
|
||||
}
|
||||
|
@ -71,15 +71,35 @@ public class AccountServiceImpl implements AccountService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public User login(LoginUserDto user) {
|
||||
public User authenticate(LoginUserDto userDto) throws BadRequestException {
|
||||
|
||||
if (userDto.getEmail() == null ||
|
||||
userDto.getEmail().isEmpty() ||
|
||||
userDto.getPassword() == null ||
|
||||
userDto.getPassword().isEmpty()) {
|
||||
|
||||
throw new BadRequestException("Please provide a valid email and password");
|
||||
}
|
||||
|
||||
if(!userDto.getEmail().matches(InternalConfiguration.EMAIL_REGEX_RCF)) {
|
||||
throw new BadRequestException("Invalid email format");
|
||||
}
|
||||
|
||||
if(!userDto.getPassword().matches(InternalConfiguration.PASSWORD_REGEX)) {
|
||||
throw new BadRequestException("Invalid password format");
|
||||
}
|
||||
|
||||
if (!userRepository.findByEmail(userDto.getEmail()).isPresent()) {
|
||||
throw new BadRequestException("No account registered with this email");
|
||||
}
|
||||
|
||||
authenticationManager.authenticate(
|
||||
new UsernamePasswordAuthenticationToken(
|
||||
user.getEmail(),
|
||||
user.getPassword()));
|
||||
userDto.getEmail(),
|
||||
userDto.getPassword()));
|
||||
|
||||
return userRepository.findByEmail(user.getEmail())
|
||||
return userRepository.findByEmail(userDto.getEmail())
|
||||
.orElseThrow();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user