using System.Security.Claims; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using LoopFit.Application.DTOs.Auth; using LoopFit.Application.Interfaces; namespace LoopFit.Web.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] RegisterRequest request) { var result = await _authService.RegisterAsync(request); if (result is null) return BadRequest(new { message = "Registrierung fehlgeschlagen. Email existiert bereits oder Passwort ist nicht gültig." }); return Ok(result); } [HttpPost("login")] public async Task> Login([FromBody] LoginRequest request) { var result = await _authService.LoginAsync(request); if (result is null) return Unauthorized(new { message = "Email oder Passwort falsch." }); return Ok(result); } [Authorize] [HttpGet("me")] public async Task> GetCurrentUser() { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); if (userId is null) return Unauthorized(); var result = await _authService.GetCurrentUserAsync(Guid.Parse(userId)); if (result is null) return NotFound(); return Ok(result); } }