48 lines
1.5 KiB
C#
48 lines
1.5 KiB
C#
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");
|
|
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));
|
|
}
|
|
}
|
|
}
|