2024-05-31 13:16:09 +00:00
|
|
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
|
|
|
using Neighbourhood.omg.lol.Models;
|
|
|
|
|
using System.Security.Claims;
|
2024-06-01 04:38:12 +00:00
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Text.Json.Serialization;
|
2024-05-31 13:16:09 +00:00
|
|
|
|
|
|
|
|
|
namespace Neighbourhood.omg.lol {
|
|
|
|
|
public class CustomAuthenticationStateProvider : AuthenticationStateProvider {
|
2024-06-01 04:38:12 +00:00
|
|
|
|
private State State;
|
|
|
|
|
public CustomAuthenticationStateProvider(State _state) {
|
|
|
|
|
this.State = _state;
|
2024-05-31 13:16:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
2024-06-01 04:38:12 +00:00
|
|
|
|
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
|
2024-05-31 13:16:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override async Task<AuthenticationState> GetAuthenticationStateAsync() {
|
|
|
|
|
var identity = new ClaimsIdentity();
|
|
|
|
|
try {
|
|
|
|
|
var token = await SecureStorage.GetAsync("accounttoken");
|
|
|
|
|
if (token != null) {
|
2024-06-01 04:38:12 +00:00
|
|
|
|
await State.PopulateAccountDetails(token);
|
2024-05-31 13:16:09 +00:00
|
|
|
|
|
2024-06-01 04:38:12 +00:00
|
|
|
|
if(State.AccountInfo != null) {
|
|
|
|
|
List<Claim> claims = new List<Claim> {
|
|
|
|
|
new Claim(ClaimTypes.Name, State.AccountInfo.Name),
|
|
|
|
|
new Claim(ClaimTypes.Email, State.AccountInfo.Email)
|
2024-05-31 13:16:09 +00:00
|
|
|
|
};
|
|
|
|
|
identity = new ClaimsIdentity(claims, "Server authentication");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (HttpRequestException ex) {
|
|
|
|
|
Console.WriteLine("Request failed:" + ex.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new AuthenticationState(new ClaimsPrincipal(identity));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|