diff --git a/Controllers/AuthController.cs b/Controllers/AuthController.cs index 2a216b9..f0793f6 100644 --- a/Controllers/AuthController.cs +++ b/Controllers/AuthController.cs @@ -3,7 +3,6 @@ using Microsoft.AspNetCore.Mvc; using QuotifyBE.Data; using QuotifyBE.Entities; using QuotifyBE.DTOs; -using System.Threading.Tasks; using QuotifyBE.Mapping; using Microsoft.AspNetCore.Cors; @@ -83,8 +82,11 @@ public class AuthController : ControllerBase /// /// /// Dummy, authed endpoint used to test JWTs. + ///

+ /// Important! /// Authed endpoints expect Authorization header, e.g.: - /// Authorization: bearer {jwt}
+ /// Authorization: bearer {jwt} + /// /// Dummy json /// Returned on request with valid credentials /// Returned on request with invalid JWT @@ -104,8 +106,7 @@ public class AuthController : ControllerBase /// /// /// Authed endpoint used to check human-readable user role. - /// Authed endpoints expect Authorization header, e.g.: - /// Authorization: bearer {jwt} + /// /// Json containing single field "role" /// Returned on request with valid credentials /// Returned on request with JWT whose user could not be found (sanity check) @@ -125,4 +126,31 @@ public class AuthController : ControllerBase return Ok(new { Role = guhf.UserRoleAsStr(u) }); } + // GET /api/v1/auth/me + /// + /// [AUTHED] Get user info + /// + /// + /// Authed endpoint used to get info about the user. + /// + /// Json containing user info DTO + /// Returned on request with valid credentials + /// Returned on request with JWT whose user could not be found (sanity check) + [HttpGet("me")] + [Authorize] + [EnableCors] + [ProducesResponseType(typeof(UserInfoDTO), 200)] + [ProducesResponseType(typeof(ErrorDTO), 400)] + public IActionResult GetUserData() + { + // Get user token from Authorization header + User? u = guhf.GetUserFromToken(Request.Headers.Authorization!); + if (u == null) // sanity check + return BadRequest(new ErrorDTO { Status = "error", Error_msg = "User not found" }); + + // Return user data as a DTO + return Ok(u.ToUserInfoDTO()); + + } + }