Class AuthService
java.lang.Object
me.josephsf.jobportaljosephsfeir.service.AuthService
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 Summary
ConstructorsConstructorDescriptionAuthService(UserRepository userRepository, org.springframework.security.crypto.password.PasswordEncoder passwordEncoder) Constructs a new AuthService with required dependencies. -
Method Summary
Modifier and TypeMethodDescriptionchangePassword(String userId, String oldPassword, String newPassword) Changes a user's password.voiddeleteUser(String userId) Deletes a user account.booleanisEmailAvailable(String email) Checks if an email is available for registration.booleanisUsernameAvailable(String username) Checks if a username is available for registration.registerUser(RegisterDto registrationRequest) Registers a new user in the system.
-
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 operationspasswordEncoder- Encoder for securely hashing passwords
-
-
Method Details
-
isUsernameAvailable
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
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
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
Changes a user's password. Verifies the old password before allowing the change.- Parameters:
userId- The ID of the user whose password is being changedoldPassword- The current password for verificationnewPassword- 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
Deletes a user account.- Parameters:
userId- The ID of the user to delete
-