migrare UI in WebAssembly + integrare Serviciu stocare info pe web + WebToken
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
using Application.Services.Database.PostgreSQL;
|
||||
using Application.Services.HashingAlgorithms;
|
||||
|
||||
namespace Application.Endpoints.Authorization.Doctor;
|
||||
|
||||
public class DoctorLoginHandler
|
||||
{
|
||||
private readonly IDoctorRepository _database;
|
||||
private readonly IHashingAlgorithms _hashingAlgorithms;
|
||||
|
||||
public DoctorLoginHandler(IDoctorRepository database, IHashingAlgorithms hashingAlgorithms)
|
||||
{
|
||||
_database = database;
|
||||
_hashingAlgorithms = hashingAlgorithms;
|
||||
}
|
||||
|
||||
public async Task<BaseResponse> Handle(LoginDto loginDTO)
|
||||
{
|
||||
loginDTO.Password = _hashingAlgorithms.SHA256Algorithm(loginDTO.Password);
|
||||
|
||||
var validation = new DoctorLoginValidation(_database);
|
||||
var validationResult = await validation.ValidateAsync(loginDTO);
|
||||
|
||||
if (validationResult.IsValid)
|
||||
{
|
||||
var doctor = await _database.FindByEmailAsync(loginDTO.Email);
|
||||
return new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.OK,
|
||||
Message = "Authentication successful",
|
||||
Data = doctor
|
||||
};
|
||||
}
|
||||
|
||||
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,33 @@
|
||||
using Application.Services.Database.PostgreSQL;
|
||||
using FluentValidation;
|
||||
|
||||
namespace Application.Endpoints.Authorization.Doctor;
|
||||
|
||||
public class DoctorLoginValidation : AbstractValidator<LoginDto>
|
||||
{
|
||||
private readonly IDoctorRepository _doctorRepository;
|
||||
|
||||
public DoctorLoginValidation(IDoctorRepository doctorRepository)
|
||||
{
|
||||
_doctorRepository = doctorRepository;
|
||||
|
||||
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 _doctorRepository.CredentialsMatch(dto.Email, dto.Password);
|
||||
return code;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using Application.Services.Database.PostgreSQL;
|
||||
using Application.Services.HashingAlgorithms;
|
||||
|
||||
namespace Application.Endpoints.Authorization.Doctor;
|
||||
|
||||
public class DoctorResetPasswordHandler
|
||||
{
|
||||
private readonly IDoctorRepository _doctorRepository;
|
||||
private readonly IHashingAlgorithms _hashingAlgorithms;
|
||||
|
||||
public DoctorResetPasswordHandler(IDoctorRepository doctorRepository, IHashingAlgorithms hashingAlgorithms)
|
||||
{
|
||||
_doctorRepository = doctorRepository;
|
||||
_hashingAlgorithms = hashingAlgorithms;
|
||||
}
|
||||
|
||||
public async Task<BaseResponse> Handle(LoginDto resetDoctorDto)
|
||||
{
|
||||
var validation = new DoctorResetPasswordValidation(_doctorRepository, _hashingAlgorithms);
|
||||
var validationResult = await validation.ValidateAsync(resetDoctorDto);
|
||||
|
||||
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 currentDoctor = await _doctorRepository.FindByEmailAsync(resetDoctorDto.Email);
|
||||
var updatedDoctor = currentDoctor;
|
||||
updatedDoctor.SetPassword(_hashingAlgorithms.SHA256Algorithm(resetDoctorDto.Password));
|
||||
|
||||
await _doctorRepository.UpdateAsync(updatedDoctor);
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.OK,
|
||||
Message = "Password successfully changed!",
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using System.Net;
|
||||
using Application.Services.Database.PostgreSQL;
|
||||
using Application.Services.HashingAlgorithms;
|
||||
using FluentValidation;
|
||||
|
||||
namespace Application.Endpoints.Authorization.Doctor;
|
||||
|
||||
public class DoctorResetPasswordValidation : AbstractValidator<LoginDto>
|
||||
{
|
||||
private readonly IDoctorRepository _doctorRepository;
|
||||
private readonly IHashingAlgorithms _hashingAlgorithms;
|
||||
|
||||
public DoctorResetPasswordValidation(IDoctorRepository doctorRepository, IHashingAlgorithms hashingAlgorithms)
|
||||
{
|
||||
_doctorRepository = doctorRepository;
|
||||
_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(BeExistingDoctor).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> BeExistingDoctor(string email, CancellationToken cancellationToken)
|
||||
{
|
||||
var doctor = await _doctorRepository.FindByEmailAsync(email);
|
||||
return doctor != null;
|
||||
}
|
||||
|
||||
private async Task<bool> BeDifferentFromOldPassword(LoginDto dto, CancellationToken cancellationToken)
|
||||
{
|
||||
var currentPatient = await _doctorRepository.FindByEmailAsync(dto.Email);
|
||||
return !_hashingAlgorithms.SHA256Algorithm(dto.Password)
|
||||
.Equals(currentPatient?.Password, StringComparison.Ordinal);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user