Commit 13b39d39 authored by Almouhannad's avatar Almouhannad

(B) Modify JWT provider and login response

parent 9bec7b49
using Domain.Entities.Identity.Users; using Domain.Entities.Identity.Users;
using Domain.Entities.People.Shared;
namespace Application.Abstractions.JWT; namespace Application.Abstractions.JWT;
public interface IJWTProvider public interface IJWTProvider
{ {
string Generate(User user); string Generate(User user, PersonalInfo? personalInfo = null);
} }
...@@ -33,37 +33,38 @@ public class LoginCommandHandler : CommandHandlerBase<LoginCommand, LoginRespons ...@@ -33,37 +33,38 @@ public class LoginCommandHandler : CommandHandlerBase<LoginCommand, LoginRespons
return Result.Failure<LoginResponse>(IdentityErrors.PasswordMismatch); return Result.Failure<LoginResponse>(IdentityErrors.PasswordMismatch);
#endregion #endregion
#region 2. Generate JWT
User user = loginResult.Value!; User user = loginResult.Value!;
string token = _jwtProvider.Generate(user);
#endregion
#region 3. Generate Response #region 2. Generate Response
#region 3.1. Admin #region 2.1. Admin
if (user.Role == Roles.Admin) if (user.Role == Roles.Admin)
{ {
return LoginResponse.GetResponse(user, token); var token = _jwtProvider.Generate(user);
return LoginResponse.GetResponse(token);
} }
#endregion #endregion
#region 3.2. Doctor #region 2.2. Doctor
if (user.Role == Roles.Doctor) if (user.Role == Roles.Doctor)
{ {
var doctorUserResult = await _userRepository.GetDoctorUserByUserNameFullAsync(user.UserName); var doctorUserResult = await _userRepository.GetDoctorUserByUserNameFullAsync(user.UserName);
if (doctorUserResult.IsFailure) if (doctorUserResult.IsFailure)
return Result.Failure<LoginResponse>(doctorUserResult.Error); return Result.Failure<LoginResponse>(doctorUserResult.Error);
return LoginResponse.GetResponse(doctorUserResult.Value.User, token, doctorUserResult.Value.Doctor.PersonalInfo);
var token = _jwtProvider.Generate(user, doctorUserResult.Value.Doctor.PersonalInfo);
return LoginResponse.GetResponse(token);
} }
#endregion #endregion
#region 3.3. Receptionist user #region 2.3. Receptionist user
if (user.Role == Roles.Receptionist) if (user.Role == Roles.Receptionist)
{ {
var receptionistUser = await _userRepository.GetReceptionistUserByUserNameFullAsync(user.UserName); var receptionistUser = await _userRepository.GetReceptionistUserByUserNameFullAsync(user.UserName);
if (receptionistUser.IsFailure) if (receptionistUser.IsFailure)
return Result.Failure<LoginResponse>(receptionistUser.Error); return Result.Failure<LoginResponse>(receptionistUser.Error);
return LoginResponse.GetResponse(receptionistUser.Value.User, token, receptionistUser.Value.PersonalInfo); var token = _jwtProvider.Generate(user, receptionistUser.Value.PersonalInfo);
return LoginResponse.GetResponse(token);
} }
#endregion #endregion
......
...@@ -8,27 +8,16 @@ namespace Application.Users.Commands.Login; ...@@ -8,27 +8,16 @@ namespace Application.Users.Commands.Login;
public class LoginResponse public class LoginResponse
{ {
public int Id { get; set; }
public string UserName { get; set; } = null!;
public string JWT { get; set; } = null!; public string JWT { get; set; } = null!;
public string FullName { get; set; } = null!;
public static Result<LoginResponse> GetResponse(User user, string jwt, PersonalInfo? personalInfo = null) public static Result<LoginResponse> GetResponse(string jwt)
{ {
if (jwt is null)
return Result.Failure< LoginResponse>(IdentityErrors.NotFound);
var response = new LoginResponse var response = new LoginResponse
{ {
Id = user.Id,
UserName = user.UserName,
JWT = jwt JWT = jwt
}; };
if (personalInfo is null)
{
if (user.Role != Roles.Admin)
return Result.Failure<LoginResponse>(IdentityErrors.NotFound);
response.FullName = Roles.AdminName;
return response;
}
response.FullName = personalInfo.FullName;
return response; return response;
} }
} }
using Application.Abstractions.JWT; using Application.Abstractions.JWT;
using Domain.Entities.Identity.Users; using Domain.Entities.Identity.Users;
using Domain.Entities.People.Shared;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt; using System.IdentityModel.Tokens.Jwt;
...@@ -17,15 +18,20 @@ public sealed class JWTProvider : IJWTProvider ...@@ -17,15 +18,20 @@ public sealed class JWTProvider : IJWTProvider
_options = options.Value; _options = options.Value;
} }
public string Generate(User user) public string Generate(User user, PersonalInfo? personalInfo = null)
{ {
var claims = new Claim[] var claims = new List<Claim>
{ {
new("Id", user.Id.ToString()),
new(ClaimTypes.Name, user.UserName), new(ClaimTypes.Name, user.UserName),
new(ClaimTypes.Role, user.Role.Name) new(ClaimTypes.Role, user.Role.Name)
}; };
if (personalInfo is not null)
{
claims.Add(new Claim("FullName", personalInfo.FullName));
}
var signingCredentials = new SigningCredentials( var signingCredentials = new SigningCredentials(
new SymmetricSecurityKey( new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(_options.SecretKey)), Encoding.UTF8.GetBytes(_options.SecretKey)),
...@@ -34,7 +40,7 @@ public sealed class JWTProvider : IJWTProvider ...@@ -34,7 +40,7 @@ public sealed class JWTProvider : IJWTProvider
var token = new JwtSecurityToken( var token = new JwtSecurityToken(
_options.Issuer, _options.Issuer,
_options.Audience, _options.Audience,
claims, claims.ToArray(),
null, null,
DateTime.UtcNow.AddDays(30), DateTime.UtcNow.AddDays(30),
signingCredentials); signingCredentials);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment