From 91c9ab6dfebc7015a46ef4b62de8fff505dfa3c1 Mon Sep 17 00:00:00 2001 From: Nour Eldien Ayman Date: Sun, 4 May 2025 23:07:37 +0300 Subject: [PATCH 1/2] Add endpoint to fetch current user details; implement getCurrentUserDetails method in AuthenticationService --- .../controller/AuthenticationController.java | 17 +++++++++++++++ .../auth/service/AuthenticationService.java | 21 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/main/java/com/podzilla/auth/controller/AuthenticationController.java b/src/main/java/com/podzilla/auth/controller/AuthenticationController.java index f441f96..1c45c26 100644 --- a/src/main/java/com/podzilla/auth/controller/AuthenticationController.java +++ b/src/main/java/com/podzilla/auth/controller/AuthenticationController.java @@ -12,10 +12,12 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.security.core.userdetails.UserDetails; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.GetMapping; @RestController @RequestMapping("/auth") @@ -101,4 +103,19 @@ public ResponseEntity refreshToken( "User " + email + " refreshed tokens successfully", HttpStatus.OK); } + + @GetMapping("/me") + @Operation( + summary = "Get Current User", + description = "Fetches the details of the currently logged-in user." + ) + @ApiResponse( + responseCode = "200", + description = "User details fetched successfully" + ) + public UserDetails getCurrentUser() { + UserDetails userDetails = authenticationService.getCurrentUserDetails(); + LOGGER.info("Fetched details for user {}", userDetails.getUsername()); + return userDetails; + } } diff --git a/src/main/java/com/podzilla/auth/service/AuthenticationService.java b/src/main/java/com/podzilla/auth/service/AuthenticationService.java index ae40a1e..7f96d11 100644 --- a/src/main/java/com/podzilla/auth/service/AuthenticationService.java +++ b/src/main/java/com/podzilla/auth/service/AuthenticationService.java @@ -122,6 +122,20 @@ public String refreshToken(final HttpServletRequest request, } } + public UserDetails getCurrentUserDetails() { + checkUserNotLoggedIn("User is not logged in."); + + Authentication authentication = + SecurityContextHolder.getContext().getAuthentication(); + Object principal = authentication.getPrincipal(); + if (principal instanceof UserDetails) { + return (UserDetails) principal; + } else { + throw new InvalidActionException( + "User details not saved correctly."); + } + } + private void checkNotNullValidationException(final String value, final String message) { if (value == null || value.isEmpty()) { @@ -149,4 +163,11 @@ private void checkUserLoggedIn(final String message) { throw new InvalidActionException(message); } } + + private void checkUserNotLoggedIn(final String message) { + if (!(SecurityContextHolder.getContext().getAuthentication() + instanceof UsernamePasswordAuthenticationToken)) { + throw new InvalidActionException(message); + } + } } From 1979f3503917ef5700dd9df1b4e7cabcd5c68ffb Mon Sep 17 00:00:00 2001 From: Nour Eldien Ayman Date: Sun, 4 May 2025 23:09:53 +0300 Subject: [PATCH 2/2] Refactor getCurrentUserDetails method; remove unnecessary user login check --- .../podzilla/auth/service/AuthenticationService.java | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/main/java/com/podzilla/auth/service/AuthenticationService.java b/src/main/java/com/podzilla/auth/service/AuthenticationService.java index 7f96d11..038a35c 100644 --- a/src/main/java/com/podzilla/auth/service/AuthenticationService.java +++ b/src/main/java/com/podzilla/auth/service/AuthenticationService.java @@ -123,10 +123,9 @@ public String refreshToken(final HttpServletRequest request, } public UserDetails getCurrentUserDetails() { - checkUserNotLoggedIn("User is not logged in."); - Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + Object principal = authentication.getPrincipal(); if (principal instanceof UserDetails) { return (UserDetails) principal; @@ -163,11 +162,4 @@ private void checkUserLoggedIn(final String message) { throw new InvalidActionException(message); } } - - private void checkUserNotLoggedIn(final String message) { - if (!(SecurityContextHolder.getContext().getAuthentication() - instanceof UsernamePasswordAuthenticationToken)) { - throw new InvalidActionException(message); - } - } }