mirror of
https://github.com/QuotifyTeam/QuotifyBE.git
synced 2025-12-17 01:00:07 +01:00
feat: helper functions for checking roles and a demo endpoint
This commit is contained in:
@@ -98,4 +98,31 @@ public class AuthController : ControllerBase
|
||||
return Ok(new string[] { "value1", "value2" });
|
||||
}
|
||||
|
||||
// GET /api/v1/auth/user_role
|
||||
/// <summary>
|
||||
/// [AUTHED] Get user role as a string
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Authed endpoint used to check human-readable user role.
|
||||
/// Authed endpoints expect Authorization header, e.g.:
|
||||
/// Authorization: bearer {jwt}</remarks>
|
||||
/// <returns>Json containing single field "role"</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("user_role")]
|
||||
[Authorize]
|
||||
[EnableCors]
|
||||
[ProducesResponseType(200)]
|
||||
[ProducesResponseType(typeof(ErrorDTO), 400)]
|
||||
public IActionResult GetUserRole()
|
||||
{
|
||||
// Get user from token
|
||||
User? u = guhf.GetUserFromToken(Request.Headers.Authorization!);
|
||||
if (u == null)
|
||||
return BadRequest(new ErrorDTO { Status = "error", Error_msg = "User not found" });
|
||||
|
||||
// Return the role as a string
|
||||
return Ok(new { Role = guhf.UserRoleAsStr(u) });
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user