migrare UI in WebAssembly + integrare Serviciu stocare info pe web + WebToken

This commit is contained in:
andrei-mihnea-cerbu
2024-04-10 06:57:48 +03:00
parent 9977a9b5f5
commit adba50e70b
1915 changed files with 16113 additions and 99991 deletions
@@ -0,0 +1,46 @@
using System.Net;
using Application.Services.Database.PostgreSQL;
using Application.Services.HashingAlgorithms;
using FluentValidation;
namespace Application.Endpoints.Authorization.Patient;
public class PatientResetPasswordValidation : AbstractValidator<LoginDto>
{
private readonly IPatientRepository _patientRepository;
private readonly IHashingAlgorithms _hashingAlgorithms;
public PatientResetPasswordValidation(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())
.MustAsync(BeExistingPatient).WithMessage("Patient with this email does not exist.")
.WithErrorCode(HttpStatusCodes.NotFound.ToString());
RuleFor(x => x.Password)
.NotEmpty().WithMessage("Password is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.WithErrorCode(HttpStatusCode.BadRequest.ToString());
RuleFor(x => x)
.MustAsync(BeDifferentFromOldPassword).WithMessage("Password can't be as the previous.")
.WithErrorCode(HttpStatusCode.BadRequest.ToString()).WithErrorCode(HttpStatusCodes.BadRequest.ToString());
}
private async Task<bool> BeExistingPatient(string email, CancellationToken cancellationToken)
{
var patient = await _patientRepository.FindByEmailAsync(email);
return patient != null;
}
private async Task<bool> BeDifferentFromOldPassword(LoginDto dto, CancellationToken cancellationToken)
{
var currentPatient = await _patientRepository.FindByEmailAsync(dto.Email);
return !_hashingAlgorithms.SHA256Algorithm(dto.Password)
.Equals(currentPatient?.Password, StringComparison.Ordinal);
}
}