migrare UI in WebAssembly + integrare Serviciu stocare info pe web + WebToken
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
using Application.Services.Database.PostgreSQL;
|
||||
using Application.Services.HashingAlgorithms;
|
||||
|
||||
namespace Application.Endpoints.Authorization.Patient;
|
||||
|
||||
public class PatientLoginHandler
|
||||
{
|
||||
private readonly IPatientRepository _patientRepository;
|
||||
private readonly IHashingAlgorithms _hashingAlgorithms;
|
||||
|
||||
public PatientLoginHandler(IPatientRepository patientRepository, IHashingAlgorithms hashingAlgorithms)
|
||||
{
|
||||
_patientRepository = patientRepository;
|
||||
_hashingAlgorithms = hashingAlgorithms;
|
||||
}
|
||||
|
||||
public async Task<BaseResponse> Handle(LoginDto loginDTO)
|
||||
{
|
||||
var validation = new PatientLoginValidation(_patientRepository, _hashingAlgorithms);
|
||||
var validationResult = await validation.ValidateAsync(loginDTO);
|
||||
|
||||
if (validationResult.IsValid)
|
||||
{
|
||||
var patient = await _patientRepository.FindByEmailAsync(loginDTO.Email);
|
||||
return new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.OK,
|
||||
Message = "Authentication successful",
|
||||
Data = patient
|
||||
};
|
||||
}
|
||||
|
||||
var firstError = validationResult.Errors.FirstOrDefault();
|
||||
var errorCode = int.TryParse(firstError.ErrorCode, out var code) ? code : -1;
|
||||
var errorMessage = firstError.ErrorMessage;
|
||||
return new BaseResponse
|
||||
{
|
||||
StatusCode = errorCode,
|
||||
Message = errorMessage,
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using Application.Services.Database.PostgreSQL;
|
||||
using Application.Services.HashingAlgorithms;
|
||||
|
||||
namespace Application.Endpoints.Authorization.Patient;
|
||||
|
||||
public class PatientResetPasswordHandler
|
||||
{
|
||||
private readonly IHashingAlgorithms _hashingAlgorithms;
|
||||
private readonly IPatientRepository _patientRepository;
|
||||
|
||||
public PatientResetPasswordHandler(IPatientRepository patientRepository, IHashingAlgorithms hashingAlgorithms)
|
||||
{
|
||||
_patientRepository = patientRepository;
|
||||
_hashingAlgorithms = hashingAlgorithms;
|
||||
}
|
||||
|
||||
public async Task<BaseResponse> Handle(LoginDto patientResetPasswordDto)
|
||||
{
|
||||
var validation = new PatientResetPasswordValidation(_patientRepository, _hashingAlgorithms);
|
||||
var validationResult = await validation.ValidateAsync(patientResetPasswordDto);
|
||||
|
||||
if (!validationResult.IsValid)
|
||||
{
|
||||
var firstError = validationResult.Errors.FirstOrDefault();
|
||||
var errorCode = int.TryParse(firstError.ErrorCode, out var code) ? code : -1;
|
||||
var errorMessage = firstError.ErrorMessage;
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
StatusCode = errorCode,
|
||||
Message = errorMessage,
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
var currentPatient = await _patientRepository.FindByEmailAsync(patientResetPasswordDto.Email);
|
||||
var updatedPatient = currentPatient;
|
||||
updatedPatient.SetPassword(_hashingAlgorithms.SHA256Algorithm(patientResetPasswordDto.Password));
|
||||
|
||||
await _patientRepository.UpdateAsync(updatedPatient);
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.OK,
|
||||
Message = "Password successfully changed!",
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user