@inject AuthenticationStateProvider AuthProvider @inject IUserService UserService @inject IUserContextAccessor UserContext @inject ILogger Logger @using Microsoft.AspNetCore.Components.Authorization @using SharepointToolbox.Web.Services.Auth @using SharepointToolbox.Web.Services.Session @* Invisible component. Run once per circuit to seed IUserContextAccessor. *@ @code { protected override async Task OnInitializedAsync() { var state = await AuthProvider.GetAuthenticationStateAsync(); var principal = state.User; if (principal.Identity?.IsAuthenticated != true) { Logger.LogWarning("AppInitializer: circuit principal NOT authenticated; UserContext left unseeded → page stays on loading."); return; } var email = principal.FindFirst("preferred_username")?.Value ?? principal.FindFirst(System.Security.Claims.ClaimTypes.Email)?.Value; if (string.IsNullOrEmpty(email)) { var claims = string.Join(", ", principal.Claims.Select(c => $"{c.Type}={c.Value}")); Logger.LogWarning("AppInitializer: authenticated but no preferred_username/email claim. Claims present: [{Claims}]", claims); return; } var user = await UserService.GetByEmailAsync(email); if (user is null) { Logger.LogWarning("AppInitializer: no user row for email '{Email}' — provisioning did not persist a matching record.", email); return; } Logger.LogInformation("AppInitializer: seeded UserContext for '{Email}' (role {Role}).", user.Email, user.Role); UserContext.Initialize(user); } }