Package com.nicholasalfonso.lato.service
Class AuthService
java.lang.Object
com.nicholasalfonso.lato.service.AuthService
Service for account registration, authentication, session tokens, and logout flows.
-
Constructor Summary
ConstructorsConstructorDescriptionAuthService(UserRepository userRepository, org.springframework.security.crypto.password.PasswordEncoder passwordEncoder, JwtService jwtService, RefreshTokenService refreshTokenService, RefreshTokenRepository refreshTokenRepository) Creates the authentication service with required security dependencies. -
Method Summary
Modifier and TypeMethodDescriptionauthenticateCredentials(LoginRequest request, jakarta.servlet.http.HttpServletResponse response) Authenticates a user with email and password credentials.createSessionToken(User user, jakarta.servlet.http.HttpServletResponse response) Creates access and refresh tokens for a user and sets the refresh cookie.createUserAccount(RegisterRequest request, jakarta.servlet.http.HttpServletResponse response) Registers a new user account and starts an authenticated session.deleteUserAccount(String token, jakarta.servlet.http.HttpServletResponse response) Deletes the authenticated user account and clears the refresh token cookie.encryptPassword(String plainPassword) Hashes a plain-text password for storage.refreshToken(String token, jakarta.servlet.http.HttpServletResponse response) Rotates the refresh token and issues a new access token.revokeRefreshToken(String refreshToken, jakarta.servlet.http.HttpServletResponse response) Revokes the refresh token and clears the refresh token cookie.
-
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 persistencepasswordEncoder- encoder for password hashingjwtService- service for access token generationrefreshTokenService- service for refresh token lifecycle managementrefreshTokenRepository- 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 detailsresponse- 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 cookieresponse- HTTP response used to clear the refresh token cookie- Returns:
- confirmation message
-
refreshToken
Rotates the refresh token and issues a new access token.- Parameters:
token- current refresh token from the request cookieresponse- 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 credentialsresponse- 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 cookieresponse- HTTP response used to clear the refresh token cookie- Returns:
- confirmation message
-
encryptPassword
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 userresponse- HTTP response used to set the refresh token cookie- Returns:
- authentication response containing the access token and user profile
-