View Javadoc
1   package fr.tiogars.domaintemplate.domains.auth.controllers;
2   
3   import java.security.Principal;
4   
5   import org.springframework.http.ResponseEntity;
6   import org.springframework.web.bind.annotation.GetMapping;
7   import org.springframework.web.bind.annotation.RestController;
8   
9   import fr.tiogars.domaintemplate.domains.auth.user.entities.User;
10  import fr.tiogars.domaintemplate.domains.auth.user.services.UserService;
11  import io.swagger.v3.oas.annotations.Operation;
12  import io.swagger.v3.oas.annotations.security.SecurityRequirement;
13  import io.swagger.v3.oas.annotations.tags.Tag;
14  
15  /** Exposes the current authenticated user. */
16  @Tag(name = "Authentication", description = "JWT authentication endpoints.")
17  @RestController
18  public class AuthMeController {
19  
20      private final UserService service;
21  
22      AuthMeController(UserService service) {
23          this.service = service;
24      }
25  
26      @Operation(summary = "Current user", description = "Returns the authenticated user.")
27      @SecurityRequirement(name = "bearerAuth")
28      @GetMapping("/api/v1/auth/me")
29      ResponseEntity<User> me(Principal principal) {
30          return service.findByUsername(principal.getName())
31                  .map(ResponseEntity::ok)
32                  .orElseGet(() -> ResponseEntity.notFound().build());
33      }
34  }