using System.Security.Claims;
using Microsoft.AspNetCore.Components.Authorization;

namespace WishNinja.Services;

public static class AuthStateExtensions
{
    /// <summary>Returns the current user's Identity id, or null if not authenticated.</summary>
    public static async Task<string?> GetUserIdAsync(this AuthenticationStateProvider provider)
    {
        var state = await provider.GetAuthenticationStateAsync();
        return state.User.FindFirstValue(ClaimTypes.NameIdentifier);
    }

    public static async Task<bool> IsInRoleAsync(this AuthenticationStateProvider provider, string role)
    {
        var state = await provider.GetAuthenticationStateAsync();
        return state.User.IsInRole(role);
    }
}
