|
10 | 10 | import com.zenfulcode.commercify.auth.domain.exception.InvalidAuthenticationException; |
11 | 11 | import com.zenfulcode.commercify.auth.domain.model.AuthenticatedUser; |
12 | 12 | import com.zenfulcode.commercify.shared.interfaces.ApiResponse; |
| 13 | +import com.zenfulcode.commercify.user.application.dto.response.UserProfileResponse; |
13 | 14 | import com.zenfulcode.commercify.user.application.service.UserApplicationService; |
| 15 | +import com.zenfulcode.commercify.user.domain.model.User; |
14 | 16 | import lombok.RequiredArgsConstructor; |
15 | 17 | import lombok.extern.slf4j.Slf4j; |
16 | 18 | import org.springframework.http.HttpStatus; |
|
24 | 26 | public class AuthController { |
25 | 27 | private final AuthenticationApplicationService authService; |
26 | 28 | private final UserApplicationService userService; |
| 29 | + private final UserApplicationService userApplicationService; |
27 | 30 |
|
28 | 31 | @PostMapping("/nextauth") |
29 | 32 | public ResponseEntity<ApiResponse<NextAuthResponse>> nextAuthSignIn(@RequestBody LoginRequest request) { |
@@ -52,10 +55,26 @@ public ResponseEntity<ApiResponse<NextAuthResponse>> validateSession(@RequestHea |
52 | 55 | } |
53 | 56 | } |
54 | 57 |
|
| 58 | + @GetMapping("/me") |
| 59 | + public ResponseEntity<ApiResponse<UserProfileResponse>> getCurrentUser(@RequestHeader("Authorization") String authHeader) { |
| 60 | + // Extract token using a domain service method |
| 61 | + String token = authService.extractTokenFromHeader(authHeader).orElseThrow(() -> new InvalidAuthenticationException("Invalid authorization header")); |
| 62 | + |
| 63 | + // Validate token through the application service |
| 64 | + AuthenticatedUser authenticatedUser = authService.validateAccessToken(token); |
| 65 | + |
| 66 | + // Fetch full user entity from the database |
| 67 | + User user = userApplicationService.getUser(authenticatedUser.getUserId()); |
| 68 | + |
| 69 | + // Map to response DTO |
| 70 | + UserProfileResponse response = UserProfileResponse.fromUser(user); |
| 71 | + |
| 72 | + return ResponseEntity.ok(ApiResponse.success(response)); |
| 73 | + } |
| 74 | + |
55 | 75 | @PostMapping("/signin") |
56 | 76 | public ResponseEntity<ApiResponse<AuthResponse>> login(@RequestBody LoginRequest request) { |
57 | 77 | AuthenticationResult result = authService.authenticate(request.toCommand()); |
58 | | - |
59 | 78 | AuthResponse response = AuthResponse.from(result); |
60 | 79 | return ResponseEntity.ok(ApiResponse.success(response)); |
61 | 80 | } |
|
0 commit comments