finalizare 1.0
This commit is contained in:
@@ -1,148 +1,68 @@
|
||||
using Application.Endpoints;
|
||||
using Application.Endpoints.Authorization;
|
||||
using Application.Endpoints.Authorization.Doctor;
|
||||
using Application.Endpoints.Authorization.Patient;
|
||||
using Application.Endpoints.Authorization.RefreshToken;
|
||||
using Application.Endpoints.Authorization.UserLogin;
|
||||
using Application.Endpoints.Authorization.UserRegister;
|
||||
using Application.Endpoints.Authorization.UserResetPassword;
|
||||
using Application.Services.Database.PostgreSQL;
|
||||
using Application.Services.Email;
|
||||
using Application.Services.HashingAlgorithms;
|
||||
using Application.Services.Jwt;
|
||||
using Core.Entities;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace API.Controllers;
|
||||
|
||||
[Route("api/[controller]")]
|
||||
public class AuthorizationController : BaseApiController
|
||||
public class AuthorizationController(
|
||||
IEmailService emailService,
|
||||
IJwtService jwtService,
|
||||
IPatientRepository patientRepository,
|
||||
IDoctorRepository doctorRepository,
|
||||
IAdminRepository adminRepository,
|
||||
IHashingAlgorithms hashingAlgorithms)
|
||||
: BaseApiController
|
||||
{
|
||||
private readonly IJwtService _jwtService;
|
||||
private readonly IEmailService _emailService;
|
||||
private readonly IPatientRepository _patientRepository;
|
||||
private readonly IDoctorRepository _doctorRepository;
|
||||
private readonly IHashingAlgorithms _hashingAlgorithms;
|
||||
|
||||
public AuthorizationController(IEmailService emailService, IJwtService jwtService,
|
||||
IPatientRepository patientRepository, IDoctorRepository doctorRepository,
|
||||
IHashingAlgorithms hashingAlgorithms)
|
||||
{
|
||||
_jwtService = jwtService;
|
||||
_emailService = emailService;
|
||||
_patientRepository = patientRepository;
|
||||
_doctorRepository = doctorRepository;
|
||||
_hashingAlgorithms = hashingAlgorithms;
|
||||
}
|
||||
|
||||
[Authorize(Policy = Policies.AnonymousPolicy)]
|
||||
[HttpPost("login")]
|
||||
public async Task<ActionResult<BaseResponse>> Login(UserLoginModel dto)
|
||||
public async Task<ActionResult<BaseResponse>> Login(UserLoginCommand request, CancellationToken token)
|
||||
{
|
||||
BaseResponse? response = null;
|
||||
var loginInfo = new LoginDto();
|
||||
loginInfo.Email = dto.Email;
|
||||
loginInfo.Password = dto.Password;
|
||||
|
||||
switch (dto.UserType)
|
||||
{
|
||||
case "doctor":
|
||||
var handlerDoctor = new DoctorLoginHandler(_doctorRepository, _hashingAlgorithms);
|
||||
response = await handlerDoctor.Handle(loginInfo).ConfigureAwait(false);
|
||||
|
||||
if (response.Data != null)
|
||||
{
|
||||
Doctor patient = (Doctor)response.Data;
|
||||
var authToken = _jwtService.GenerateJwtToken(patient.Email);
|
||||
Console.WriteLine(patient.Email);
|
||||
var handler = new UserLoginHandler(jwtService, hashingAlgorithms, doctorRepository,
|
||||
patientRepository, adminRepository);
|
||||
|
||||
HttpContext.Response.Headers.Add("Authorization", $"Bearer {authToken}");
|
||||
}
|
||||
break;
|
||||
case "patient":
|
||||
var handlerPatient = new PatientLoginHandler(_patientRepository, _hashingAlgorithms);
|
||||
response = await handlerPatient.Handle(loginInfo).ConfigureAwait(false);
|
||||
|
||||
if (response.Data != null)
|
||||
{
|
||||
Patient patient = (Patient)response.Data;
|
||||
var authToken = _jwtService.GenerateJwtToken(patient.Email);
|
||||
|
||||
HttpContext.Response.Headers.Add("Authorization", $"Bearer {authToken}");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return new ActionResult<BaseResponse>(new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.BadRequest,
|
||||
Message = "Need user type.",
|
||||
Data = null
|
||||
});
|
||||
}
|
||||
|
||||
return StatusCode(response.StatusCode, response);
|
||||
var result = await handler.Handle(request, token);
|
||||
return StatusCode(result.StatusCode, result);
|
||||
}
|
||||
|
||||
[Authorize(Policy = Policies.AnonymousPolicy)]
|
||||
[HttpPost("register")]
|
||||
public async Task<ActionResult<BaseResponse>> Register(UserRegisterCommand request, CancellationToken token)
|
||||
{
|
||||
var handler = new UserRegisterHandler(emailService, hashingAlgorithms, doctorRepository,
|
||||
patientRepository, adminRepository);
|
||||
|
||||
var result = await handler.Handle(request, token);
|
||||
return StatusCode(result.StatusCode, result);
|
||||
}
|
||||
|
||||
[Authorize(Policy = Policies.AnonymousPolicy)]
|
||||
[HttpPost("reset_password")]
|
||||
public async Task<ActionResult<BaseResponse>> ResetPassword(UserLoginModel dto)
|
||||
public async Task<ActionResult<BaseResponse>> ResetPassword(UserResetPasswordCommand request,
|
||||
CancellationToken token)
|
||||
{
|
||||
BaseResponse? response = null;
|
||||
var loginInfo = new LoginDto();
|
||||
loginInfo.Email = dto.Email;
|
||||
loginInfo.Password = dto.Password;
|
||||
|
||||
switch (dto.UserType)
|
||||
{
|
||||
case "doctor":
|
||||
var handlerDoctor = new DoctorResetPasswordHandler(_doctorRepository, _hashingAlgorithms);
|
||||
response = await handlerDoctor.Handle(loginInfo).ConfigureAwait(false);
|
||||
break;
|
||||
case "patient":
|
||||
var handlerPatient = new PatientResetPasswordHandler(_patientRepository, _hashingAlgorithms);
|
||||
response = await handlerPatient.Handle(loginInfo).ConfigureAwait(false);
|
||||
break;
|
||||
default:
|
||||
return new ActionResult<BaseResponse>(new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.BadRequest,
|
||||
Message = "Need user type.",
|
||||
Data = null
|
||||
});
|
||||
}
|
||||
var handler = new UserResetPasswordHandler(emailService, hashingAlgorithms, doctorRepository,
|
||||
patientRepository, adminRepository);
|
||||
|
||||
if (response.StatusCode < HttpStatusCodes.BadRequest)
|
||||
{
|
||||
var body = _emailService.GenerateResetCredentialsEmailBody(
|
||||
loginInfo.Email, loginInfo.Password);
|
||||
await _emailService.SendEmailAsync(loginInfo.Email, "Password reset successfully!", body);
|
||||
}
|
||||
|
||||
return StatusCode(response.StatusCode, response);
|
||||
var result = await handler.Handle(request, token);
|
||||
return StatusCode(result.StatusCode, result);
|
||||
}
|
||||
|
||||
|
||||
[Authorize(Policy = Policies.AnonymousPolicy)]
|
||||
[HttpPost("refresh_token")]
|
||||
public async Task<ActionResult<BaseResponse>> RefreshToken()
|
||||
public async Task<ActionResult<BaseResponse>> RefreshToken([FromBody] RefreshJwtCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var authorizationHeader = Request.Headers["Authorization"].FirstOrDefault();
|
||||
if (string.IsNullOrEmpty(authorizationHeader) || !authorizationHeader.StartsWith("Bearer "))
|
||||
return StatusCode(HttpStatusCodes.BadRequest, new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.BadRequest,
|
||||
Message = "Invalid request header format.",
|
||||
Data = null
|
||||
});
|
||||
|
||||
var oldToken = authorizationHeader.Substring("Bearer ".Length).Trim();
|
||||
|
||||
if (!_jwtService.ValidateJwtToken(oldToken))
|
||||
return StatusCode(HttpStatusCodes.Unauthorized, new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.Unauthorized,
|
||||
Message = "Invalid JWT token.",
|
||||
Data = null
|
||||
});
|
||||
|
||||
var newToken = _jwtService.RefreshToken(oldToken);
|
||||
return StatusCode(HttpStatusCodes.OK, new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.OK,
|
||||
Message = "Token refreshed successfully.",
|
||||
Data = new { Token = newToken }
|
||||
});
|
||||
var handler = new RefreshJwtHandler(jwtService);
|
||||
var result = await handler.Handle(request, cancellationToken);
|
||||
return StatusCode(result.StatusCode, result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user