Spaces:
Runtime error
Runtime error
File size: 1,122 Bytes
de8b914 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | using SCM.Application.Authentication.Dtos;
using SCM.Domain.Users;
namespace SCM.Application.Authentication;
public sealed class AuthService : IAuthService
{
private readonly IUserRepository _users;
private readonly IPasswordHasherService _passwordHasher;
private readonly IJwtTokenGenerator _tokenGenerator;
public AuthService(
IUserRepository users,
IPasswordHasherService passwordHasher,
IJwtTokenGenerator tokenGenerator)
{
_users = users;
_passwordHasher = passwordHasher;
_tokenGenerator = tokenGenerator;
}
public async Task<LoginResponse> LoginAsync(LoginRequest request, CancellationToken ct = default)
{
var user = await _users.GetByUsernameAsync(request.Username, ct);
if (user is null || !user.IsActive || !_passwordHasher.Verify(user.PasswordHash, request.Password))
throw new UnauthorizedAccessException("Usuário ou senha inválidos.");
var (token, expiresAt) = _tokenGenerator.GenerateToken(user);
return new LoginResponse(token, expiresAt, user.Username, user.Role);
}
}
|