Neighbourhood.omg.lol/Classes/CustomAuthenticationStateProvider.cs

46 lines
1.4 KiB
C#
Raw Permalink Normal View History

using Microsoft.AspNetCore.Components.Authorization;
using System.Security.Claims;
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");
2024-06-05 12:41:08 +00:00
await State.RemoveAccountDetails();
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
}
public override async Task<AuthenticationState> GetAuthenticationStateAsync() {
var identity = new ClaimsIdentity();
try {
var token = await SecureStorage.GetAsync("accounttoken");
if (token != null) {
await State.PopulateAccountDetails(token);
if(State.AccountInfo != null) {
List<Claim> claims = new List<Claim> {
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));
}
}
}