package de.itsolutions.ticketsystem.controller; import de.itsolutions.ticketsystem.dto.Dtos; import de.itsolutions.ticketsystem.entity.User; import de.itsolutions.ticketsystem.service.AuthService; import org.springframework.http.ResponseEntity; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; import java.security.Principal; /** * REST controller for authentication-related operations. * Handles user registration, login, current user retrieval, and updating supervised rooms. */ @RestController @RequestMapping("/api/auth") public class AuthController { private final AuthService authService; private final AuthenticationManager authenticationManager; /** * Constructs an AuthController with necessary services. * @param authService The authentication service. * @param authenticationManager The authentication manager. */ public AuthController(AuthService authService, AuthenticationManager authenticationManager) { this.authService = authService; this.authenticationManager = authenticationManager; } /** * Registers a new user in the system. * @param request The registration request containing user details. * @return A ResponseEntity with the registered user. */ @PostMapping("/register") public ResponseEntity register(@RequestBody Dtos.RegisterRequest request) { return ResponseEntity.ok(authService.register(request)); } /** * Authenticates a user and returns their details upon successful login. * @param request The login request containing user credentials. * @return A ResponseEntity with the authenticated user's details. */ @PostMapping("/login") public ResponseEntity login(@RequestBody Dtos.LoginRequest request) { Authentication authentication = authenticationManager.authenticate( new UsernamePasswordAuthenticationToken(request.getEmail(), request.getPassword()) ); return ResponseEntity.ok(authService.getUserByEmail(request.getEmail())); } /** * Retrieves the currently authenticated user's information. * @param principal The security principal representing the authenticated user. * @return A ResponseEntity with the current user's details. */ @GetMapping("/me") public ResponseEntity getCurrentUser(Principal principal) { return ResponseEntity.ok(authService.getUserByEmail(principal.getName())); } /** * Updates the list of rooms supervised by the current user. * @param request The request containing the IDs of rooms to supervise. * @param principal The security principal of the current user. * @return A ResponseEntity with the updated user details. */ @PutMapping("/profile/rooms") public ResponseEntity updateMyRooms(@RequestBody Dtos.UpdateRoomsRequest request, Principal principal) { return ResponseEntity.ok(authService.updateSupervisedRooms(principal.getName(), request.getRoomIds())); } }