From 15f5a9ed31e89c9e0fed2330e84f3b981f9ac8f9 Mon Sep 17 00:00:00 2001 From: Ted Wollman <25165500+TheTedder@users.noreply.github.com> Date: Mon, 24 Jun 2024 19:14:01 -0400 Subject: [PATCH] Update GET /users/me to fix issues. (#212) --- .../Controllers/UsersController.cs | 30 +++++++------------ LeaderboardBackend/openapi.json | 12 +++++++- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/LeaderboardBackend/Controllers/UsersController.cs b/LeaderboardBackend/Controllers/UsersController.cs index 0ff2a3a9..47baa4b5 100644 --- a/LeaderboardBackend/Controllers/UsersController.cs +++ b/LeaderboardBackend/Controllers/UsersController.cs @@ -50,29 +50,19 @@ public async Task> GetUserById(Guid id) /// Example: `{ 'Authorization': 'Bearer JWT' }`. /// /// The `User` was found and returned successfully.. - /// An invalid JWT was passed in. + /// An invalid JWT was passed in. + /// The user was not found in the database. [HttpGet("me")] [ProducesResponseType(StatusCodes.Status200OK)] - [ProducesResponseType(StatusCodes.Status403Forbidden)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [ApiConventionMethod(typeof(Conventions), nameof(Conventions.Get))] public async Task> Me() { - // FIXME: Use ApiConventionMethod here! - Ero - - string? email = _authService.GetEmailFromClaims(HttpContext.User); - - if (email is null) - { - return Forbid(); - } - - User? user = await _userService.GetUserByEmail(email); - - // FIXME: Should return NotFound()! - Ero - if (user is null) - { - return Forbid(); - } - - return Ok(UserViewModel.MapFrom(user)); + return (await _userService.GetUserFromClaims(HttpContext.User)).Match>( + user => Ok(UserViewModel.MapFrom(user)), + badCredentials => Unauthorized(), + userNotFound => NotFound() + ); } } diff --git a/LeaderboardBackend/openapi.json b/LeaderboardBackend/openapi.json index 95aa114e..5b4f9b6a 100644 --- a/LeaderboardBackend/openapi.json +++ b/LeaderboardBackend/openapi.json @@ -1047,7 +1047,7 @@ } } }, - "403": { + "401": { "description": "An invalid JWT was passed in.", "content": { "application/json": { @@ -1056,6 +1056,16 @@ } } } + }, + "404": { + "description": "The user was not found in the database.", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } } } }