2026-05-21 11:04:42 +02:00
|
|
|
using System.Security.Claims;
|
|
|
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
|
|
|
|
|
|
|
|
namespace AuthorBuddy.Web.Services;
|
|
|
|
|
|
|
|
|
|
public class CustomAuthStateProvider : AuthenticationStateProvider
|
|
|
|
|
{
|
|
|
|
|
private readonly IAuthService _authService;
|
|
|
|
|
private AuthenticationState _anonymous;
|
|
|
|
|
|
|
|
|
|
public CustomAuthStateProvider(IAuthService authService)
|
|
|
|
|
{
|
|
|
|
|
_authService = authService;
|
|
|
|
|
_anonymous = new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
|
|
|
|
|
{
|
|
|
|
|
var user = await _authService.GetCurrentUserAsync();
|
|
|
|
|
if (user != null)
|
|
|
|
|
{
|
2026-06-21 07:52:03 +02:00
|
|
|
var claims = new List<Claim>
|
2026-05-21 11:04:42 +02:00
|
|
|
{
|
|
|
|
|
new Claim(ClaimTypes.Name, user.Name),
|
|
|
|
|
new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
|
2026-06-21 07:52:03 +02:00
|
|
|
};
|
|
|
|
|
foreach (var role in user.Roles)
|
|
|
|
|
claims.Add(new Claim(ClaimTypes.Role, role.ToString()));
|
|
|
|
|
|
|
|
|
|
var identity = new ClaimsIdentity(claims, "custom");
|
2026-05-21 11:04:42 +02:00
|
|
|
return new AuthenticationState(new ClaimsPrincipal(identity));
|
|
|
|
|
}
|
|
|
|
|
return _anonymous;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void NotifyStateChanged()
|
|
|
|
|
{
|
|
|
|
|
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
|
|
|
|
|
}
|
|
|
|
|
}
|