Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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;
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/podzilla/auth/service/AuthenticationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ public String refreshToken(final HttpServletRequest request,
}
}

public UserDetails getCurrentUserDetails() {
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()) {
Expand Down