This commit is contained in:
andrei-mihnea-cerbu
2024-04-08 13:37:58 +03:00
parent 333ad8be09
commit 370bbdedcd
164 changed files with 3915 additions and 161 deletions
+55 -5
View File
@@ -5,6 +5,8 @@ using Application.Endpoints.Doctors.Registration;
using Application.Endpoints.Doctors.ResetPassword;
using Application.Services.Database;
using Application.Services.HashingAlgorithms;
using Application.Services.Jwt;
using Core.Entities;
using Microsoft.AspNetCore.Mvc;
namespace API.Controllers;
@@ -15,29 +17,77 @@ public class DoctorsController : ControllerBase
{
private readonly IDoctorRepository _database;
private readonly IHashingAlgorithms _hashingAlgorithms;
private readonly IJwtService _jwtService;
public DoctorsController(IDoctorRepository database, IHashingAlgorithms hashingAlgorithms)
public DoctorsController(IDoctorRepository database, IHashingAlgorithms hashingAlgorithms,
IJwtService jwtService)
{
_database = database;
_hashingAlgorithms = hashingAlgorithms;
_jwtService = jwtService;
}
[HttpPost("register")]
public async Task<ActionResult<BaseResponse>> Register(DoctorRegistrationDto doctor)
public async Task<ActionResult<BaseResponse>> Register(DoctorRegistrationDto doctorRegistrationDto)
{
var handler = new DoctorRegistrationHandler(_database, _hashingAlgorithms);
var response = await handler.Handle(doctor).ConfigureAwait(false);
var response = await handler.Handle(doctorRegistrationDto).ConfigureAwait(false);
return StatusCode(response.StatusCode, response);
}
[HttpPost("login")]
public async Task<ActionResult<BaseResponse>> Login(DoctorLoginDto doctor)
public async Task<ActionResult<BaseResponse>> Login(DoctorLoginDto doctorLoginDto)
{
var handler = new DoctorLoginHandler(_database, _hashingAlgorithms);
var response = await handler.Handle(doctor).ConfigureAwait(false);
var response = await handler.Handle(doctorLoginDto).ConfigureAwait(false);
if (response.Data != null)
{
Doctor doctor = (Doctor)response.Data;
var authToken = _jwtService.GenerateJwtToken(doctor.Email);
HttpContext.Response.Headers.Add("Authorization", $"Bearer {authToken}");
}
return StatusCode(response.StatusCode, response);
}
[HttpPost("refresh_token")]
public async Task<ActionResult<BaseResponse>> RefreshToken()
{
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
});
}
else
{
var newToken = _jwtService.RefreshToken(oldToken);
return StatusCode(HttpStatusCodes.OK, new BaseResponse
{
StatusCode = HttpStatusCodes.OK,
Message = "Token refreshed successfully.",
Data = new { Token = newToken }
});
}
}
[HttpPost("reset_password")]
public async Task<ActionResult<BaseResponse>> ResetPassword(DoctorResetPasswordDto resetDoctorDto)
{