Class AuthenticationService

java.lang.Object
app.YUP.service.AuthenticationService

@Service public class AuthenticationService extends Object
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
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    authenticate(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.
    void
    logout(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
    Logs out a user by deleting the JWT cookie.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 password
      response - the HTTP response to set the JWT cookie
      Throws:
      InvalidParameterException - if the username or password is invalid
      ResourceNotFoundException - 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 object
      response - the HTTP response object
      Throws:
      CookieNotFoundException - if the JWT cookie is not found
    • getIdFromCookie

      public String getIdFromCookie(jakarta.servlet.http.HttpServletRequest request)
      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

      public String checkIdFromCookie(jakarta.servlet.http.HttpServletRequest request)
      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

      public String getFirstNameFromCookie(jakarta.servlet.http.HttpServletRequest request)
      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