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 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); } }