mirror of
https://github.com/Govor-team/Govor.git
synced 2026-09-17 16:22:49 +00:00
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.
This commit is contained in:
@@ -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<IActionResult> 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user