AdminPanel/NetinaShop.AdminPanel.PWA/Services/CustomAuthenticationStatePr...

34 lines
1.0 KiB
C#

using System.Security.Claims;
using Microsoft.AspNetCore.Components.Authorization;
namespace NetinaShop.AdminPanel.PWA.Services;
public class CustomAuthenticationStateProvider : AuthenticationStateProvider
{
private readonly IUserUtility _userUtility;
public CustomAuthenticationStateProvider(IUserUtility userUtility)
{
_userUtility = userUtility;
}
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
var token = await _userUtility.GetBearerTokenAsync();
if (token.IsNullOrEmpty())
{
return new AuthenticationState(new());
}
var user = await _userUtility.GetUserAsync();
var identity = new ClaimsIdentity(new[]
{
new Claim(ClaimTypes.Name, user.FirstName + " " + user.LastName),
new Claim(ClaimTypes.MobilePhone , user.PhoneNumber)
}, "Bearer");
var claimUser = new ClaimsPrincipal(identity);
return new AuthenticationState(claimUser);
}
}