using System; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Identity; using HyperCube.Models; using Console = HyperCube.Utils.AdvConsole; namespace HyperCube.Shared { public partial class Sidebar : ComponentBase { [Inject] public AuthenticationStateProvider AuthenticationStateProvider { get; set; } [Inject] public UserManager UserManager { get; set; } AccountModel account; List roles; //string rr = ""; //roles = account.roles; protected override async Task OnInitializedAsync() { account = await GetCurrentAcc(); if (account != null) account.RolesChanged += Change; } public async Task InitializeAccount() { AccountModel.Current = await GetCurrentAcc(); Console.WriteLine("InitializeAccount in Sidebar " + AccountModel.Current.Name); await Rerender(); } private async Task GetCurrentAcc() { AccountModel account = new(); var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); var user = authState.User; if (user.Identity.IsAuthenticated) { var currentUser = await UserManager.GetUserAsync(user); account.UUID = currentUser.Id; //account.Name = currentUser.UserName; //account.Email = currentUser.Email; var acc = AccountModel.Find(account.UUID); if (acc != null) account = acc; ///tmp //account.AccRole = Role.User; //account.Name = "test"; return account; } return null; } private void Change(int count) { Console.WriteLine($"Sidebar role changed, count: {count}"); Rerender(); } private async Task Rerender() { StateHasChanged(); } } }