using Microsoft.AspNetCore.Components.Authorization; using Neighbourhood.omg.lol.Models; using System.Security.Claims; using System.Text.Json; using System.Text.Json.Serialization; namespace Neighbourhood.omg.lol { public class CustomAuthenticationStateProvider : AuthenticationStateProvider { private State State; public CustomAuthenticationStateProvider(State _state) { this.State = _state; } public async Task Login(string token) { await SecureStorage.SetAsync("accounttoken", token); NotifyAuthenticationStateChanged(GetAuthenticationStateAsync()); } public async Task Logout() { SecureStorage.Remove("accounttoken"); Preferences.Default.Clear(); NotifyAuthenticationStateChanged(GetAuthenticationStateAsync()); } public override async Task GetAuthenticationStateAsync() { var identity = new ClaimsIdentity(); try { var token = await SecureStorage.GetAsync("accounttoken"); if (token != null) { await State.PopulateAccountDetails(token); if(State.AccountInfo != null) { List claims = new List { new Claim(ClaimTypes.Name, State.AccountInfo.Name), new Claim(ClaimTypes.Email, State.AccountInfo.Email) }; identity = new ClaimsIdentity(claims, "Server authentication"); } } } catch (HttpRequestException ex) { Console.WriteLine("Request failed:" + ex.ToString()); } return new AuthenticationState(new ClaimsPrincipal(identity)); } } }