using DotBased.ASP.Auth.Domains; using DotBased.ASP.Auth.Domains.Auth; using DotBased.ASP.Auth.Domains.Identity; using DotBased.Extensions; using DotBased.Logging; namespace DotBased.ASP.Auth.Services; public class AuthService { public AuthService(IAuthDataRepository authDataRepository, AuthDataCache dataCache) { _authDataRepository = authDataRepository; _dataCache = dataCache; _logger = LogService.RegisterLogger(typeof(AuthService)); } private readonly IAuthDataRepository _authDataRepository; private readonly AuthDataCache _dataCache; private readonly ILogger _logger; public async Task> LoginAsync(LoginModel login) { UserModel? user = null; Result usrResult; if (!login.UserName.IsNullOrWhiteSpace()) { usrResult = await _authDataRepository.GetUserAsync(string.Empty, string.Empty, login.UserName); if (usrResult is { Success: true, Value: not null }) user = usrResult.Value; } else if (!login.Email.IsNullOrWhiteSpace()) { usrResult = await _authDataRepository.GetUserAsync(string.Empty, login.Email, string.Empty); if (usrResult is { Success: true, Value: not null }) user = usrResult.Value; } else return Result.Failed("Username & Email is empty, cannot login!"); if (user == null || !usrResult.Success) return new Result(usrResult); if (user.PasswordHash != login.Password) //TODO: Hash password and compare return Result.Failed("Login failed, invalid password."); var state = new AuthenticationStateModel(user); await _authDataRepository.CreateAuthenticationStateAsync(state); return Result.Ok(state); } public async Task Logout(string state) { if (state.IsNullOrWhiteSpace()) return Result.Failed($"Argument {nameof(state)} is empty!"); /*var stateResult = await _dataProvider.GetAuthenticationStateAsync(state); if (!stateResult.Success || stateResult.Value == null) return stateResult; var authState = stateResult.Value; //TODO: Update state to logged out and update the state var updatedStateResult = await _dataProvider.UpdateAuthenticationStateAsync(authState); if (updatedStateResult.Success) return updatedStateResult; _logger.Warning(updatedStateResult.Message); return updatedStateResult;*/ return Result.Failed($"Argument {nameof(state)} is empty!"); // <- TEMP } }