[CHANGE] Reworking Result monads
This commit is contained in:
@@ -24,10 +24,10 @@ public class SecurityService
|
||||
private readonly ProtectedLocalStorage _localStorage;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public async Task<Result<AuthenticationState>> GetAuthenticationStateFromSessionAsync(string id)
|
||||
public async Task<ResultOld<AuthenticationState>> GetAuthenticationStateFromSessionAsync(string id)
|
||||
{
|
||||
if (id.IsNullOrEmpty())
|
||||
return Result<AuthenticationState>.Failed("No valid id!");
|
||||
return ResultOld<AuthenticationState>.Failed("No valid id!");
|
||||
AuthenticationStateModel? authStateModel = null;
|
||||
var stateCache = _dataCache.RequestSessionState(id);
|
||||
if (!stateCache.Success || stateCache.Value == null)
|
||||
@@ -42,16 +42,16 @@ public class SecurityService
|
||||
else
|
||||
{
|
||||
if (stateCache.Value.Item2 != null)
|
||||
return Result<AuthenticationState>.Ok(stateCache.Value.Item2);
|
||||
return ResultOld<AuthenticationState>.Ok(stateCache.Value.Item2);
|
||||
authStateModel = stateCache.Value.Item1;
|
||||
}
|
||||
|
||||
if (authStateModel == null)
|
||||
return Result<AuthenticationState>.Failed("Failed to get auth state!");
|
||||
return ResultOld<AuthenticationState>.Failed("Failed to get auth state!");
|
||||
|
||||
var userResult = await _authDataRepository.GetUserAsync(authStateModel.UserId, string.Empty, string.Empty);
|
||||
if (userResult is not { Success: true, Value: not null })
|
||||
return Result<AuthenticationState>.Failed("Failed to get user from state!");
|
||||
return ResultOld<AuthenticationState>.Failed("Failed to get user from state!");
|
||||
var claims = new List<Claim>()
|
||||
{
|
||||
new(ClaimTypes.Sid, userResult.Value.Id),
|
||||
@@ -66,56 +66,56 @@ public class SecurityService
|
||||
var claimsIdentity = new ClaimsIdentity(claims, BasedAuthDefaults.AuthenticationScheme);
|
||||
var authState = new AuthenticationState(new ClaimsPrincipal(claimsIdentity));
|
||||
_dataCache.CacheSessionState(authStateModel, authState);
|
||||
return Result<AuthenticationState>.Ok(authState);
|
||||
return ResultOld<AuthenticationState>.Ok(authState);
|
||||
}
|
||||
|
||||
public async Task<Result<AuthenticationStateModel>> LoginAsync(LoginModel login)
|
||||
public async Task<ResultOld<AuthenticationStateModel>> LoginAsync(LoginModel login)
|
||||
{
|
||||
try
|
||||
{
|
||||
UserModel? user = null;
|
||||
Result<UserModel> usrResult;
|
||||
ResultOld<UserModel> usrResultOld;
|
||||
if (!login.UserName.IsNullOrEmpty())
|
||||
{
|
||||
usrResult = await _authDataRepository.GetUserAsync(string.Empty, string.Empty, login.UserName);
|
||||
if (usrResult is { Success: true, Value: not null })
|
||||
user = usrResult.Value;
|
||||
usrResultOld = await _authDataRepository.GetUserAsync(string.Empty, string.Empty, login.UserName);
|
||||
if (usrResultOld is { Success: true, Value: not null })
|
||||
user = usrResultOld.Value;
|
||||
}
|
||||
else if (!login.Email.IsNullOrEmpty())
|
||||
{
|
||||
usrResult = await _authDataRepository.GetUserAsync(string.Empty, login.Email, string.Empty);
|
||||
if (usrResult is { Success: true, Value: not null })
|
||||
user = usrResult.Value;
|
||||
usrResultOld = await _authDataRepository.GetUserAsync(string.Empty, login.Email, string.Empty);
|
||||
if (usrResultOld is { Success: true, Value: not null })
|
||||
user = usrResultOld.Value;
|
||||
}
|
||||
else
|
||||
return Result<AuthenticationStateModel>.Failed("Username & Email is empty, cannot login!");
|
||||
return ResultOld<AuthenticationStateModel>.Failed("Username & Email is empty, cannot login!");
|
||||
|
||||
if (user == null || !usrResult.Success)
|
||||
return Result<AuthenticationStateModel>.Failed("No user found!");
|
||||
if (user == null || !usrResultOld.Success)
|
||||
return ResultOld<AuthenticationStateModel>.Failed("No user found!");
|
||||
|
||||
if (user.PasswordHash != login.Password) //TODO: Hash password and compare
|
||||
return Result<AuthenticationStateModel>.Failed("Login failed, invalid password.");
|
||||
return ResultOld<AuthenticationStateModel>.Failed("Login failed, invalid password.");
|
||||
var state = new AuthenticationStateModel(user);
|
||||
var authResult = await _authDataRepository.CreateAuthenticationStateAsync(state);
|
||||
if (!authResult.Success)
|
||||
return Result<AuthenticationStateModel>.Failed("Failed to store session to database!");
|
||||
return ResultOld<AuthenticationStateModel>.Failed("Failed to store session to database!");
|
||||
_dataCache.CacheSessionState(state);
|
||||
await _localStorage.SetAsync(BasedAuthDefaults.StorageKey, state.Id);
|
||||
return Result<AuthenticationStateModel>.Ok(state);
|
||||
return ResultOld<AuthenticationStateModel>.Ok(state);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Error(e, "Failed to login!");
|
||||
return Result<AuthenticationStateModel>.Failed("Login failed, exception thrown!");
|
||||
return ResultOld<AuthenticationStateModel>.Failed("Login failed, exception thrown!");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Result> LogoutAsync(string state)
|
||||
public async Task<ResultOld> LogoutAsync(string state)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (state.IsNullOrEmpty())
|
||||
return Result.Failed($"Argument {nameof(state)} is empty!");
|
||||
return ResultOld.Failed($"Argument {nameof(state)} is empty!");
|
||||
|
||||
var stateResult = await _authDataRepository.GetAuthenticationStateAsync(state);
|
||||
if (!stateResult.Success || stateResult.Value == null)
|
||||
@@ -131,7 +131,7 @@ public class SecurityService
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Error(e, "Failed to logout!");
|
||||
return Result.Failed("Failed to logout, exception thrown!");
|
||||
return ResultOld.Failed("Failed to logout, exception thrown!");
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user