28 lines
774 B
C#
28 lines
774 B
C#
|
|
using SIGCM.Application.Interfaces;
|
||
|
|
using SIGCM.Domain.Interfaces;
|
||
|
|
|
||
|
|
namespace SIGCM.Infrastructure.Services;
|
||
|
|
|
||
|
|
public class AuthService : IAuthService
|
||
|
|
{
|
||
|
|
private readonly IUserRepository _userRepo;
|
||
|
|
private readonly ITokenService _tokenService;
|
||
|
|
|
||
|
|
public AuthService(IUserRepository userRepo, ITokenService tokenService)
|
||
|
|
{
|
||
|
|
_userRepo = userRepo;
|
||
|
|
_tokenService = tokenService;
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<string?> LoginAsync(string username, string password)
|
||
|
|
{
|
||
|
|
var user = await _userRepo.GetByUsernameAsync(username);
|
||
|
|
if (user == null) return null;
|
||
|
|
|
||
|
|
bool valid = BCrypt.Net.BCrypt.Verify(password, user.PasswordHash);
|
||
|
|
if (!valid) return null;
|
||
|
|
|
||
|
|
return _tokenService.GenerateToken(user);
|
||
|
|
}
|
||
|
|
}
|