using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Shared.Contracts; using JwtRegisteredClaimNames = System.IdentityModel.Tokens.Jwt.JwtRegisteredClaimNames; namespace API.Core.Controllers; /// /// Sample controller demonstrating an endpoint that requires JWT authentication. /// [ApiController] [Route("api/[controller]")] [Authorize(AuthenticationSchemes = "JWT")] public class ProtectedController : ControllerBase { /// /// Returns the authenticated caller's user ID and username extracted from the JWT claims. /// /// userId and username may be null if not present in the token. [HttpGet] public ActionResult> Get() { string? userId = User.FindFirst(JwtRegisteredClaimNames.Sub)?.Value; string? username = User.FindFirst(JwtRegisteredClaimNames.UniqueName)?.Value; return Ok( new ResponseBody { Message = "Protected endpoint accessed successfully", Payload = new { userId, username }, } ); } }