I'm trying to authenticate my user given password with the one that is hashed in the DB, but I think I'm comparing the both given? Is there a better (or proper) way to do this?
I'm also not sure what exception I should use for the password not matching.
Controller code -
@PostMapping(path = "/login")
public Object login(User user) throws UsernameNotFoundException {
User existingUser = userService.findUserByEmail(user.getEmail());
if (existingUser.getEmail() == null || existingUser.getEmail().equals("")) {
return new UsernameNotFoundException("User not found");
}
String password = user.getPassword();
BCryptPasswordEncoder bcryptEncoder = new BCryptPasswordEncoder();
String hashedPassword = "";
boolean isPasswordMatched = bcryptEncoder.matches(password, hashedPassword);
if (!isPasswordMatched) {
return new UsernameNotFoundException("Credentials don't match");
} else {
return existingUser;
}
}