From 27fdcf85daf68f81a15c037bc120df5d09ac2a81 Mon Sep 17 00:00:00 2001 From: Artemy <109195690+stalcker2288969@users.noreply.github.com> Date: Sat, 25 Jul 2026 20:26:34 +0700 Subject: [PATCH] Use Result.ToActionResult in RefreshController Import the API extensions and replace manual result handling in RefreshController with result.ToActionResult(), removing the inline empty-token check and switch-based error mapping. Also normalize the empty-token error message in UserSessionRefresher from "cannot be empty" to "can't be empty". This centralizes Result -> IActionResult conversion and simplifies the controller logic. --- .../Authentication/RefreshController.cs | 20 ++----------------- .../UserSessions/UserSessionRefresher.cs | 2 +- 2 files changed, 3 insertions(+), 19 deletions(-) diff --git a/Govor.API/Controllers/Authentication/RefreshController.cs b/Govor.API/Controllers/Authentication/RefreshController.cs index 68e81fd..4be0aa5 100644 --- a/Govor.API/Controllers/Authentication/RefreshController.cs +++ b/Govor.API/Controllers/Authentication/RefreshController.cs @@ -1,3 +1,4 @@ +using Govor.API.Common.Extensions; using Govor.Application.Users.UserSessions; using Govor.Contracts.Requests; using Govor.Contracts.Responses; @@ -26,30 +27,13 @@ public class RefreshController : Controller [HttpPost("refresh")] // api/auth/token/refresh public async Task Refresh([FromBody] RefreshTokenRequest refreshRequest) { - if (string.IsNullOrWhiteSpace(refreshRequest.RefreshToken)) - { - _logger.LogWarning("Refresh request failed: token is empty."); - return BadRequest("Refresh token can't be empty."); - } - var result = await _userSession.RefreshTokenAsync(refreshRequest.RefreshToken); if (result.IsFailure) { _logger.LogWarning("Refresh token failed. Error Code: {Code}", result.Error.Code); - - return result.Error.Code switch - { - "Auth.EmptyToken" => BadRequest(result.Error.Message), - "Auth.InvalidToken" => Unauthorized(result.Error.Message), - _ => BadRequest($"Refresh failed: {result.Error.Message}") - }; } - return Ok(new RefreshTokenResponse - { - AccessToken = result.Value.accessToken, - RefreshToken = result.Value.refreshToken - }); + return result.ToActionResult(); } } \ No newline at end of file diff --git a/Govor.Application/Users/UserSessions/UserSessionRefresher.cs b/Govor.Application/Users/UserSessions/UserSessionRefresher.cs index 6c7467b..3499d49 100644 --- a/Govor.Application/Users/UserSessions/UserSessionRefresher.cs +++ b/Govor.Application/Users/UserSessions/UserSessionRefresher.cs @@ -34,7 +34,7 @@ public class UserSessionRefresher : IUserSessionRefresher { if (string.IsNullOrWhiteSpace(refreshToken)) { - return Result.Failure(Error.Failure("Auth.EmptyToken", "Refresh token cannot be empty.")); + return Result.Failure(Error.Failure("Auth.EmptyToken", "Refresh token can't be empty.")); } var hashedToken = _jwtTokenHasher.HashToken(refreshToken);