Package app.YUP.service
Class AuthenticationService
java.lang.Object
app.YUP.service.AuthenticationService
This class represents the service layer for the Authentication entity.
It contains methods for authenticating users, logging out users, and managing JWT cookies.
The class is annotated with Spring's @Service annotation to indicate that it's a service component.
- Version:
- 1.0
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidauthenticate(AuthenticationRequest authenticationRequest, jakarta.servlet.http.HttpServletResponse response) Authenticates a user using their username and password.checkIdFromCookie(jakarta.servlet.http.HttpServletRequest request) This method retrieves the user ID from the cookie in the HttpServletRequest object.getFirstNameFromCookie(jakarta.servlet.http.HttpServletRequest request) Retrieves the first name if the user is logged in.getIdFromCookie(jakarta.servlet.http.HttpServletRequest request) Retrieves the id from the JWT cookie.voidlogout(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Logs out a user by deleting the JWT cookie.
-
Constructor Details
-
AuthenticationService
public AuthenticationService()
-
-
Method Details
-
authenticate
public void authenticate(AuthenticationRequest authenticationRequest, jakarta.servlet.http.HttpServletResponse response) Authenticates a user using their username and password. It generates a JWT token for the authenticated user and stores it in a cookie.- Parameters:
authenticationRequest- the authentication request containing the username and passwordresponse- the HTTP response to set the JWT cookie- Throws:
InvalidParameterException- if the username or password is invalidResourceNotFoundException- if the user is not found
-
logout
public void logout(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Logs out a user by deleting the JWT cookie.- Parameters:
request- the HTTP request objectresponse- the HTTP response object- Throws:
CookieNotFoundException- if the JWT cookie is not found
-
getIdFromCookie
Retrieves the id from the JWT cookie.- Parameters:
request- the HTTP request to retrieve the cookies- Returns:
- the id from the JWT cookie
- Throws:
CookieNotFoundException- if the JWT cookie is not found
-
checkIdFromCookie
This method retrieves the user ID from the cookie in the HttpServletRequest object. It first attempts to extract the user ID from the cookie using the getIdFromCookie method. If an exception occurs during the extraction process, it sets the user ID to null.- Parameters:
request- This is the HttpServletRequest object that contains the request details.- Returns:
- String This returns the user ID extracted from the cookie, or null if the extraction fails.
-
getFirstNameFromCookie
Retrieves the first name if the user is logged in.- Parameters:
request- the HTTP request to retrieve the cookies- Returns:
- the first name of the user if logged in, otherwise null
-