Class AuthService

java.lang.Object
me.josephsf.jobportaljosephsfeir.service.AuthService

@Service public class AuthService extends Object
Service class for authentication and user registration in the Job Portal system.

This service provides methods for user registration, password management, and basic authentication-related functionality. It interacts with the MongoDB database through the UserRepository and handles secure password storage using Spring Security's PasswordEncoder.

Since:
2025-05-15
  • Constructor Details

    • AuthService

      public AuthService(UserRepository userRepository, org.springframework.security.crypto.password.PasswordEncoder passwordEncoder)
      Constructs a new AuthService with required dependencies.
      Parameters:
      userRepository - Repository for user data operations
      passwordEncoder - Encoder for securely hashing passwords
  • Method Details

    • isUsernameAvailable

      public boolean isUsernameAvailable(String username)
      Checks if a username is available for registration.
      Parameters:
      username - The username to check
      Returns:
      true if the username is available, false if it already exists
    • isEmailAvailable

      public boolean isEmailAvailable(String email)
      Checks if an email is available for registration.
      Parameters:
      email - The email to check
      Returns:
      true if the email is available, false if it already exists
    • registerUser

      public User registerUser(RegisterDto registrationRequest)
      Registers a new user in the system. Validates that the username and email are available before registration. Securely hashes the password and assigns appropriate roles.
      Parameters:
      registrationRequest - The data transfer object containing registration information
      Returns:
      The newly created user account
      Throws:
      RuntimeException - if the username or email is already taken
    • changePassword

      public User changePassword(String userId, String oldPassword, String newPassword)
      Changes a user's password. Verifies the old password before allowing the change.
      Parameters:
      userId - The ID of the user whose password is being changed
      oldPassword - The current password for verification
      newPassword - The new password to set
      Returns:
      The updated user account or null if the user is not found
      Throws:
      RuntimeException - if the old password is invalid
    • deleteUser

      public void deleteUser(String userId)
      Deletes a user account.
      Parameters:
      userId - The ID of the user to delete