Class AuthService

java.lang.Object
com.nicholasalfonso.lato.service.AuthService

@Service public class AuthService extends Object
Service for account registration, authentication, session tokens, and logout flows.
  • Constructor Details

    • AuthService

      public AuthService(UserRepository userRepository, org.springframework.security.crypto.password.PasswordEncoder passwordEncoder, JwtService jwtService, RefreshTokenService refreshTokenService, RefreshTokenRepository refreshTokenRepository)
      Creates the authentication service with required security dependencies.
      Parameters:
      userRepository - repository for user persistence
      passwordEncoder - encoder for password hashing
      jwtService - service for access token generation
      refreshTokenService - service for refresh token lifecycle management
      refreshTokenRepository - repository for refresh token persistence
  • Method Details

    • createUserAccount

      @Transactional public AuthResponse createUserAccount(RegisterRequest request, jakarta.servlet.http.HttpServletResponse response)
      Registers a new user account and starts an authenticated session.
      Parameters:
      request - registration details
      response - HTTP response used to set the refresh token cookie
      Returns:
      authentication response containing the access token and user profile
      Throws:
      org.springframework.web.server.ResponseStatusException - if the email is already registered
    • deleteUserAccount

      @Transactional public MessageResponse deleteUserAccount(String token, jakarta.servlet.http.HttpServletResponse response)
      Deletes the authenticated user account and clears the refresh token cookie.
      Parameters:
      token - refresh token from the request cookie
      response - HTTP response used to clear the refresh token cookie
      Returns:
      confirmation message
    • refreshToken

      public AuthResponse refreshToken(String token, jakarta.servlet.http.HttpServletResponse response)
      Rotates the refresh token and issues a new access token.
      Parameters:
      token - current refresh token from the request cookie
      response - HTTP response used to set the new refresh token cookie
      Returns:
      authentication response containing a new access token
    • authenticateCredentials

      public AuthResponse authenticateCredentials(LoginRequest request, jakarta.servlet.http.HttpServletResponse response)
      Authenticates a user with email and password credentials.
      Parameters:
      request - login credentials
      response - HTTP response used to set the refresh token cookie
      Returns:
      authentication response containing the access token and user profile
      Throws:
      org.springframework.web.server.ResponseStatusException - if credentials are invalid
    • revokeRefreshToken

      public MessageResponse revokeRefreshToken(String refreshToken, jakarta.servlet.http.HttpServletResponse response)
      Revokes the refresh token and clears the refresh token cookie.
      Parameters:
      refreshToken - refresh token from the request cookie
      response - HTTP response used to clear the refresh token cookie
      Returns:
      confirmation message
    • encryptPassword

      public String encryptPassword(String plainPassword)
      Hashes a plain-text password for storage.
      Parameters:
      plainPassword - the password to hash
      Returns:
      encoded password hash
    • createSessionToken

      public AuthResponse createSessionToken(User user, jakarta.servlet.http.HttpServletResponse response)
      Creates access and refresh tokens for a user and sets the refresh cookie.
      Parameters:
      user - the authenticated user
      response - HTTP response used to set the refresh token cookie
      Returns:
      authentication response containing the access token and user profile