using System; using System.Security.Claims; using System.Threading.Tasks; using D3ROVoice.Api.Dtos; using D3ROVoice.Api.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace D3ROVoice.Api.Controllers; [ApiController] [Route("api/[controller]")] public class AuthController : ControllerBase { private readonly IAuthService _authService; public AuthController(IAuthService authService) { _authService = authService; } [HttpPost("register")] public async Task Register([FromBody] RegisterDto dto) { if (string.IsNullOrWhiteSpace(dto.Email) || string.IsNullOrWhiteSpace(dto.Password)) { return BadRequest(new { message = "이메일과 비밀번호를 입력해주세요." }); } try { var result = await _authService.RegisterAsync(dto); return Ok(result); } catch (InvalidOperationException ex) { return Conflict(new { message = ex.Message }); } } [HttpPost("login")] public async Task Login([FromBody] LoginDto dto) { if (string.IsNullOrWhiteSpace(dto.Email) || string.IsNullOrWhiteSpace(dto.Password)) { return BadRequest(new { message = "이메일과 비밀번호를 입력해주세요." }); } try { var result = await _authService.LoginAsync(dto); return Ok(result); } catch (UnauthorizedAccessException ex) { return Unauthorized(new { message = ex.Message }); } } [Authorize] [HttpGet("me")] public async Task GetMe() { var email = User.FindFirstValue(ClaimTypes.Email); if (string.IsNullOrEmpty(email)) return Unauthorized(); var user = await _authService.GetUserByEmailAsync(email); if (user == null) return NotFound(); return Ok(user); } }