refactor: implement Result pattern for IdentityService and add CSS fallbacks

This commit is contained in:
2026-05-10 12:18:07 +02:00
parent 24f9a2685c
commit fc68ee41ab
10 changed files with 226 additions and 116 deletions
@@ -1,4 +1,5 @@
using System.Security.Claims;
using FluentResults;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using NexusReader.Application.DTOs.User;
@@ -14,6 +15,8 @@ public class ServerIdentityService : IIdentityService
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IDbContextFactory<AppDbContext> _dbContextFactory;
public event Func<Task>? OnStateInvalidated;
public ServerIdentityService(
UserManager<NexusUser> userManager,
IHttpContextAccessor httpContextAccessor,
@@ -24,24 +27,24 @@ public class ServerIdentityService : IIdentityService
_dbContextFactory = dbContextFactory;
}
public Task<bool> LoginAsync(string email, string password, bool rememberMe = false)
public Task<Result> LoginAsync(string email, string password, bool rememberMe = false)
=> throw new NotSupportedException("Use standard Identity endpoints for login on server.");
public Task LogoutAsync()
public Task<Result> LogoutAsync()
=> throw new NotSupportedException("Use standard Identity endpoints for logout on server.");
public Task<bool> RegisterAsync(string email, string password)
public Task<Result> RegisterAsync(string email, string password)
=> throw new NotSupportedException("Use standard Identity endpoints for registration on server.");
public Task<bool> RefreshTokenAsync() => Task.FromResult(true);
public Task<Result> RefreshTokenAsync() => Task.FromResult(Result.Ok());
public async Task<UserProfile?> GetProfileAsync()
public async Task<Result<UserProfile>> GetProfileAsync()
{
var user = _httpContextAccessor.HttpContext?.User;
if (user == null || !user.Identity?.IsAuthenticated == true) return null;
if (user == null || !user.Identity?.IsAuthenticated == true) return Result.Fail("Not authenticated.");
var userId = user.FindFirstValue(ClaimTypes.NameIdentifier);
if (userId == null) return null;
if (userId == null) return Result.Fail("User ID not found.");
using var dbContext = await _dbContextFactory.CreateDbContextAsync();
@@ -73,6 +76,6 @@ public class ServerIdentityService : IIdentityService
))
.FirstOrDefaultAsync();
return profile;
return profile != null ? Result.Ok(profile) : Result.Fail("Profile not found.");
}
}