| 1 | package com.tecmilenio.mapsconect.controller; |
| 2 | |
| 3 | import com.tecmilenio.mapsconect.dto.ApiResponse; |
| 4 | import com.tecmilenio.mapsconect.dto.LoginDTO; |
| 5 | import com.tecmilenio.mapsconect.dto.RegistroDTO; |
| 6 | import com.tecmilenio.mapsconect.dto.TokenDTO; |
| 7 | import com.tecmilenio.mapsconect.service.UsuarioService; |
| 8 | import org.springframework.beans.factory.annotation.Autowired; |
| 9 | import org.springframework.http.HttpStatus; |
| 10 | import org.springframework.http.ResponseEntity; |
| 11 | import org.springframework.web.bind.annotation.*; |
| 12 | |
| 13 | import jakarta.validation.Valid; |
| 14 | |
| 15 | @RestController |
| 16 | @RequestMapping("/auth") |
| 17 | public class AuthController { |
| 18 | |
| 19 | @Autowired |
| 20 | private UsuarioService usuarioService; |
| 21 | |
| 22 | @PostMapping("/registrar") |
| 23 | public ResponseEntity<ApiResponse<TokenDTO>> registrar(@Valid @RequestBody RegistroDTO registroDTO) { |
| 24 | TokenDTO token = usuarioService.registrar(registroDTO); |
| 25 | return ResponseEntity.status(HttpStatus.CREATED) |
| 26 | .body(ApiResponse.success(token, "Usuario registrado correctamente")); |
| 27 | } |
| 28 | |
| 29 | @PostMapping("/login") |
| 30 | public ResponseEntity<ApiResponse<TokenDTO>> login(@Valid @RequestBody LoginDTO loginDTO) { |
| 31 | TokenDTO token = usuarioService.login(loginDTO); |
| 32 | return ResponseEntity.ok(ApiResponse.success(token, "Sesión iniciada correctamente")); |
| 33 | } |
| 34 | |
| 35 | } |