mirror of
https://github.com/QuotifyTeam/QuotifyBE.git
synced 2025-12-16 18:00:06 +01:00
feat: endpoint for getting user data
This commit is contained in:
@@ -3,7 +3,6 @@ using Microsoft.AspNetCore.Mvc;
|
|||||||
using QuotifyBE.Data;
|
using QuotifyBE.Data;
|
||||||
using QuotifyBE.Entities;
|
using QuotifyBE.Entities;
|
||||||
using QuotifyBE.DTOs;
|
using QuotifyBE.DTOs;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using QuotifyBE.Mapping;
|
using QuotifyBE.Mapping;
|
||||||
using Microsoft.AspNetCore.Cors;
|
using Microsoft.AspNetCore.Cors;
|
||||||
|
|
||||||
@@ -83,8 +82,11 @@ public class AuthController : ControllerBase
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// Dummy, authed endpoint used to test JWTs.
|
/// Dummy, authed endpoint used to test JWTs.
|
||||||
|
/// <br/><br/>
|
||||||
|
/// <b>Important!</b>
|
||||||
/// Authed endpoints expect Authorization header, e.g.:
|
/// Authed endpoints expect Authorization header, e.g.:
|
||||||
/// Authorization: bearer {jwt}</remarks>
|
/// Authorization: bearer {jwt}
|
||||||
|
/// </remarks>
|
||||||
/// <returns>Dummy json</returns>
|
/// <returns>Dummy json</returns>
|
||||||
/// <response code="200">Returned on request with valid credentials</response>
|
/// <response code="200">Returned on request with valid credentials</response>
|
||||||
/// <response code="401">Returned on request with invalid JWT</response>
|
/// <response code="401">Returned on request with invalid JWT</response>
|
||||||
@@ -104,8 +106,7 @@ public class AuthController : ControllerBase
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// Authed endpoint used to check human-readable user role.
|
/// Authed endpoint used to check human-readable user role.
|
||||||
/// Authed endpoints expect Authorization header, e.g.:
|
/// </remarks>
|
||||||
/// Authorization: bearer {jwt}</remarks>
|
|
||||||
/// <returns>Json containing single field "role"</returns>
|
/// <returns>Json containing single field "role"</returns>
|
||||||
/// <response code="200">Returned on request with valid credentials</response>
|
/// <response code="200">Returned on request with valid credentials</response>
|
||||||
/// <response code="400">Returned on request with JWT whose user could not be found (sanity check)</response>
|
/// <response code="400">Returned on request with JWT whose user could not be found (sanity check)</response>
|
||||||
@@ -125,4 +126,31 @@ public class AuthController : ControllerBase
|
|||||||
return Ok(new { Role = guhf.UserRoleAsStr(u) });
|
return Ok(new { Role = guhf.UserRoleAsStr(u) });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GET /api/v1/auth/me
|
||||||
|
/// <summary>
|
||||||
|
/// [AUTHED] Get user info
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Authed endpoint used to get info about the user.
|
||||||
|
/// </remarks>
|
||||||
|
/// <returns>Json containing user info DTO</returns>
|
||||||
|
/// <response code="200">Returned on request with valid credentials</response>
|
||||||
|
/// <response code="400">Returned on request with JWT whose user could not be found (sanity check)</response>
|
||||||
|
[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());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user