using System.Security.Claims; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; /// /// JWT authentication and current user info. /// [ApiController] [Route("api/v1/auth")] [Produces("application/json")] public class AuthController : ControllerBase { private readonly IAuthService _auth; public AuthController(IAuthService auth) => _auth = auth; /// /// Authenticates a user and returns a JWT with role claims. /// [HttpPost("login")] [AllowAnonymous] [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status200OK)] [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status422UnprocessableEntity)] public async Task Login([FromBody] LoginRequest req) { var result = await _auth.LoginAsync(req); return Ok(ApiResponse.Ok(result)); } /// /// Returns the current authenticated user's profile. /// [HttpGet("me")] [Authorize] [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status200OK)] [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status401Unauthorized)] public async Task Me() { var userId = Guid.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!); var user = await _auth.GetCurrentUserAsync(userId); return Ok(ApiResponse.Ok(new UserProfileResponse( user.Id, user.Username, user.FullName, user.Role.ToDbString()))); } }