finalizare 1.0
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
namespace Application.Endpoints.Authorization.RefreshToken;
|
||||
|
||||
public class RefreshJwtCommand
|
||||
{
|
||||
public string Token { get; set; } = string.Empty;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using Application.Services.Jwt;
|
||||
|
||||
namespace Application.Endpoints.Authorization.RefreshToken;
|
||||
|
||||
public class RefreshJwtHandler(IJwtService jwtService)
|
||||
{
|
||||
public async Task<BaseResponse> Handle(RefreshJwtCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
var validator = new RefreshJwtValidator(jwtService);
|
||||
var validationResult = await validator.ValidateAsync(command, cancellationToken);
|
||||
|
||||
if (!validationResult.IsValid)
|
||||
{
|
||||
var firstError = validationResult.Errors.FirstOrDefault();
|
||||
return new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.Unauthorized,
|
||||
Message = firstError?.ErrorMessage
|
||||
};
|
||||
}
|
||||
|
||||
var newToken = jwtService.RefreshToken(command.Token);
|
||||
return new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.OK,
|
||||
Message = "Token refreshed successfully.",
|
||||
Data = newToken
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using Application.Services.Jwt;
|
||||
using FluentValidation;
|
||||
|
||||
namespace Application.Endpoints.Authorization.RefreshToken;
|
||||
|
||||
public class RefreshJwtValidator : AbstractValidator<RefreshJwtCommand>
|
||||
{
|
||||
private readonly IJwtService _jwtService;
|
||||
|
||||
public RefreshJwtValidator(IJwtService jwtService)
|
||||
{
|
||||
_jwtService = jwtService;
|
||||
|
||||
RuleFor(x => x.Token)
|
||||
.NotEmpty().WithMessage("Token is required.")
|
||||
.Must(BeAValidToken).WithMessage("Token is invalid or expired.");
|
||||
}
|
||||
|
||||
private bool BeAValidToken(string token)
|
||||
{
|
||||
return _jwtService.ValidateJwtToken(token);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user