jwt v1.0
This commit is contained in:
@@ -5,6 +5,8 @@ using Application.Endpoints.Patients.Registration;
|
||||
using Application.Endpoints.Patients.ResetPassword;
|
||||
using Application.Services.Database;
|
||||
using Application.Services.HashingAlgorithms;
|
||||
using Application.Services.Jwt;
|
||||
using Core.Entities;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace API.Controllers;
|
||||
@@ -15,11 +17,14 @@ public class PatientsController : ControllerBase
|
||||
{
|
||||
private readonly IHashingAlgorithms _hashingAlgorithms;
|
||||
private readonly IPatientRepository _patientRepository;
|
||||
private readonly IJwtService _jwtService;
|
||||
|
||||
public PatientsController(IPatientRepository patientRepository, IHashingAlgorithms hashingAlgorithms)
|
||||
public PatientsController(IPatientRepository patientRepository, IHashingAlgorithms hashingAlgorithms,
|
||||
IJwtService jwtService)
|
||||
{
|
||||
_patientRepository = patientRepository;
|
||||
_hashingAlgorithms = hashingAlgorithms;
|
||||
_jwtService = jwtService;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
@@ -43,8 +48,52 @@ public class PatientsController : ControllerBase
|
||||
{
|
||||
var handler = new PatientLoginHandler(_patientRepository);
|
||||
var response = await handler.Handle(patientLoginDto).ConfigureAwait(false);
|
||||
if (response.Data != null)
|
||||
{
|
||||
Patient patient = (Patient)response.Data;
|
||||
var authToken = _jwtService.GenerateJwtToken(patient.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("register")]
|
||||
public async Task<ActionResult<BaseResponse>> Register(PatientRegistrationDto patientRegistrationDto)
|
||||
|
||||
Reference in New Issue
Block a user