finalizare 1.0

This commit is contained in:
andrei-mihnea-cerbu
2024-05-21 12:10:53 +03:00
parent f7795f7519
commit 1cc1d34003
11268 changed files with 2102399 additions and 10909 deletions
@@ -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);
}
}