52 lines
1.5 KiB
C#

using Project.Application.Interfaces;
using Project.Infrastructure.Interfaces;
namespace Project.Application.Services
{
public class AuthService : IAuthService
{
// FIELDS FOR CTOR
private IUserRepository _userRepository;
private readonly ITwoFactorAuthService _twoFactorAuthService;
// CTOR
public AuthService(IUserRepository userRepository, ITwoFactorAuthService twoFactorAuthService)
{
_userRepository = userRepository;
_twoFactorAuthService = twoFactorAuthService;
}
// AUTHENTICATE
public async Task<bool> ValidateAsync(string email, string password)
{
var user = await _userRepository.GetByEmailAsync(email);
if (user == null || user.Password != password)
{
return false;
}
// Check if 2FA is enabled
if (!string.IsNullOrEmpty(user.SecretKey))
{
return false; // 2FA is enabled and additional validation is required
}
return true;
}
// VALIDATE TWO FACTOR AUTHENTICATION
public async Task<bool> ValidateTwoFactorAsync(string email, string code)
{
var user = await _userRepository.GetByEmailAsync(email);
if (user == null || string.IsNullOrEmpty(user.SecretKey))
{
return false;
}
return await _twoFactorAuthService.ValidateCodeAsync(user.Email, code);
}
}
}