Class UserService

java.lang.Object
com.serinity.accesscontrol.service.UserService

public final class UserService extends Object
Service class for managing user registration and authentication.

This class provides methods to register new users with a profile and role, as well as to authenticate existing users using their username and password.

Since:
2026-02-03
Version:
1.0
Author:
@ZouariOmar (zouariomar20@gmail.com)
See Also:
  • Constructor Details

    • UserService

      public UserService()
  • Method Details

    • signUp

      public static ServiceResult<User> signUp(String email, String password, String confirmPassword, UserRole role)
      Registers a new user with email, password, and role.

      This method performs:

      • Input validation for email, password, confirm password, and role.
      • User creation with hashed password.
      • Profile, session, and audit log creation.
      • Persisting all entities using the corresponding repositories.
      Parameters:
      email - the user's email
      password - the user's chosen password
      confirmPassword - confirmation of the password
      role - the role assigned to the user
      Returns:
      a ServiceResult indicating success or failure with messages
    • signIn

      public static ServiceResult<User> signIn(String usernameOrEmail, String password)
      Signs in a user using their email (or username) and password.

      This method performs the following steps:

      1. Validates the email/username format.
      2. Retrieves the user from the database.
      3. Verifies the password using PasswordEncoder.
      4. Checks for an existing active session.
      5. Returns a ServiceResult containing success or error information.
      Parameters:
      usernameOrEmail - the user's email or username
      password - the plain-text password provided by the user
      Returns:
      a ServiceResult containing the authenticated User or an error
    • signInWithFace

      public static ServiceResult<User> signInWithFace(User user)
      Creates a new authenticated session for a user who was identified via face recognition, revoking any previously active session, and records the login in the audit log with action AuditAction.USER_FACE_LOGIN.
      Parameters:
      user - the User identified by the face recognition system
      Returns:
      a ServiceResult containing the authenticated User
    • sendResetMail

      public static ServiceResult<Void> sendResetMail(String email)
      Initiates a password reset flow by sending a one-time code to the user's email address.

      The generated code is cached for 10 minutes. The user must call confirmResetMail(String, String, String) with the correct code within that window to update their password.

      Parameters:
      email - the email address of the account to reset
      Returns:
      a ServiceResult indicating success or failure with a message
    • confirmResetMail

      public static ServiceResult<Void> confirmResetMail(String email, String inputCode, String newPassword)
      Confirms a password reset by verifying the one-time code and applying the new password.

      The reset code must match the one previously sent via sendResetMail(String) and must not have expired (10-minute TTL).

      Parameters:
      email - the email address of the account to reset
      inputCode - the one-time code entered by the user
      newPassword - the desired new password (must meet complexity rules)
      Returns:
      a ServiceResult indicating success or failure with a message