private void verifyUser(User user) throws UserServiceException {
   if (this.userExists(user.getUsername())) {
     throw new UsernameAlreadyExistsException(
         "Username " + user.getUsername() + " already exists");
   }
   if (this.emailExists(user.getEmail())) {
     throw new EmailAlreadyInUseException("Email " + user.getEmail() + " already exists");
   }
 }
  @Override
  public void createUser(UserDetails user) {
    User castedUser = (User) user;
    this.verifyUser(castedUser);

    castedUser.setAuthorities(this.returnUserLevelAuthorities());
    castedUser.setPassword(SecurityConfiguration.passwordEncoder.encode(castedUser.getPassword()));
    try {
      this.userRepository.save(castedUser);
    } catch (Exception e) {
      logger.error(e.getMessage());
      throw e;
    }
  }