Fix Access/Refres Token
This commit is contained in:
@@ -7,44 +7,68 @@ using AMREZ.EOP.Abstractions.Security;
|
||||
using AMREZ.EOP.Contracts.DTOs.Authentications.Login;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace AMREZ.EOP.Application.UseCases.Authentications;
|
||||
|
||||
public sealed class LoginUseCase : ILoginUseCase
|
||||
namespace AMREZ.EOP.Application.UseCases.Authentications
|
||||
{
|
||||
private readonly ITenantResolver _tenantResolver;
|
||||
private readonly IUserRepository _users;
|
||||
private readonly IPasswordHasher _hasher;
|
||||
private readonly IHttpContextAccessor _http;
|
||||
private readonly IUnitOfWork _uow;
|
||||
|
||||
public LoginUseCase(ITenantResolver r, IUserRepository u, IPasswordHasher h, IHttpContextAccessor http, IUnitOfWork uow)
|
||||
{ _tenantResolver = r; _users = u; _hasher = h; _http = http; _uow = uow; }
|
||||
|
||||
public async Task<LoginResponse?> ExecuteAsync(LoginRequest request, CancellationToken ct = default)
|
||||
public sealed class LoginUseCase : ILoginUseCase
|
||||
{
|
||||
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
|
||||
var tenant = _tenantResolver.Resolve(http, request);
|
||||
if (tenant is null) return null;
|
||||
private readonly ITenantResolver _tenantResolver;
|
||||
private readonly IUserRepository _users;
|
||||
private readonly ITenantRepository _tenants;
|
||||
private readonly IPasswordHasher _hasher;
|
||||
private readonly IHttpContextAccessor _http;
|
||||
private readonly IUnitOfWork _uow;
|
||||
|
||||
await _uow.BeginAsync(tenant, IsolationLevel.ReadCommitted, ct);
|
||||
try
|
||||
public LoginUseCase(
|
||||
ITenantResolver tenantResolver,
|
||||
IUserRepository users,
|
||||
ITenantRepository tenants,
|
||||
IPasswordHasher hasher,
|
||||
IHttpContextAccessor http,
|
||||
IUnitOfWork uow)
|
||||
{
|
||||
_tenantResolver = tenantResolver;
|
||||
_users = users;
|
||||
_tenants = tenants;
|
||||
_hasher = hasher;
|
||||
_http = http;
|
||||
_uow = uow;
|
||||
}
|
||||
|
||||
public async Task<LoginResponse?> ExecuteAsync(LoginRequest request, CancellationToken ct = default)
|
||||
{
|
||||
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
|
||||
|
||||
var platform = _tenantResolver.Resolve(http, "@platform");
|
||||
if (platform is null) return null;
|
||||
|
||||
var email = request.Email.Trim().ToLowerInvariant();
|
||||
var user = await _users.FindActiveByEmailAsync(email, ct);
|
||||
if (user is null || !_hasher.Verify(request.Password, user.PasswordHash))
|
||||
|
||||
await _uow.BeginAsync(platform, IsolationLevel.ReadCommitted, ct);
|
||||
try
|
||||
{
|
||||
var user = await _users.FindActiveByEmailAsync(email, ct);
|
||||
if (user is null || !_hasher.Verify(request.Password, user.PasswordHash))
|
||||
{
|
||||
await _uow.RollbackAsync(ct);
|
||||
return null;
|
||||
}
|
||||
|
||||
var tenantKey = await _tenants.GetTenantKeyByTenantIdAsync(user.TenantId, ct);
|
||||
if (string.IsNullOrWhiteSpace(tenantKey))
|
||||
{
|
||||
await _uow.RollbackAsync(ct);
|
||||
return null;
|
||||
}
|
||||
|
||||
await _uow.CommitAsync(ct);
|
||||
|
||||
return new LoginResponse(user.Id, user.TenantId, email, tenantKey);
|
||||
}
|
||||
catch
|
||||
{
|
||||
await _uow.RollbackAsync(ct);
|
||||
return null;
|
||||
throw;
|
||||
}
|
||||
|
||||
await _uow.CommitAsync(ct);
|
||||
|
||||
return new LoginResponse(user.Id , email, tenant.Id);
|
||||
}
|
||||
catch
|
||||
{
|
||||
await _uow.RollbackAsync(ct);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user