37 lines
1.6 KiB
C#
37 lines
1.6 KiB
C#
using Application.Services.Database.PostgreSQL;
|
|
using Application.Services.HashingAlgorithms;
|
|
using FluentValidation;
|
|
|
|
namespace Application.Endpoints.Authorization.Patient;
|
|
|
|
public class PatientLoginValidation : AbstractValidator<LoginDto>
|
|
{
|
|
private readonly IPatientRepository _patientRepository;
|
|
private readonly IHashingAlgorithms _hashingAlgorithms;
|
|
|
|
public PatientLoginValidation(IPatientRepository patientRepository, IHashingAlgorithms hashingAlgorithms)
|
|
{
|
|
_patientRepository = patientRepository;
|
|
_hashingAlgorithms = hashingAlgorithms;
|
|
|
|
RuleFor(x => x.Email)
|
|
.NotEmpty().WithMessage("Email is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
|
|
.EmailAddress().WithMessage("Invalid email format.").WithErrorCode(HttpStatusCodes.BadRequest.ToString());
|
|
|
|
RuleFor(x => x.Password)
|
|
.NotEmpty().WithMessage("Password is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
|
|
.MinimumLength(8).WithMessage("Password must be at least 8 characters long.")
|
|
.WithErrorCode(HttpStatusCodes.BadRequest.ToString());
|
|
|
|
RuleFor(x => x)
|
|
.MustAsync(CredentialsMatch).WithMessage("Invalid credentials")
|
|
.WithErrorCode(HttpStatusCodes.Unauthorized.ToString());
|
|
}
|
|
|
|
private async Task<bool> CredentialsMatch(LoginDto dto, CancellationToken cancellationToken)
|
|
{
|
|
var code = await _patientRepository.CredentialsMatch(
|
|
dto.Email, _hashingAlgorithms.SHA256Algorithm(dto.Password));
|
|
return code;
|
|
}
|
|
} |