Use OID instead of email for microsoft identifier. Fix PlayerCount route. Add created date to user table. Create spectator icon.
137 lines
4.8 KiB
C#
137 lines
4.8 KiB
C#
using Microsoft.AspNetCore.Components;
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
|
|
using Shogi.UI.Pages.Home.Api;
|
|
using Shogi.UI.Shared;
|
|
using System.Text.Json;
|
|
|
|
namespace Shogi.UI.Pages.Home.Account;
|
|
|
|
public class AccountManager
|
|
{
|
|
private readonly AccountState accountState;
|
|
private readonly IShogiApi shogiApi;
|
|
private readonly IConfiguration configuration;
|
|
private readonly ILocalStorage localStorage;
|
|
private readonly AuthenticationStateProvider authState;
|
|
private readonly NavigationManager navigation;
|
|
private readonly ShogiSocket shogiSocket;
|
|
|
|
public AccountManager(
|
|
AccountState accountState,
|
|
IShogiApi unauthenticatedClient,
|
|
IConfiguration configuration,
|
|
AuthenticationStateProvider authState,
|
|
ILocalStorage localStorage,
|
|
NavigationManager navigation,
|
|
ShogiSocket shogiSocket)
|
|
{
|
|
this.accountState = accountState;
|
|
this.shogiApi = unauthenticatedClient;
|
|
this.configuration = configuration;
|
|
this.authState = authState;
|
|
this.localStorage = localStorage;
|
|
this.navigation = navigation;
|
|
this.shogiSocket = shogiSocket;
|
|
}
|
|
|
|
private User? MyUser { get => accountState.User; set => accountState.User = value; }
|
|
|
|
public async Task LoginWithGuestAccount()
|
|
{
|
|
var response = await shogiApi.GetToken(WhichAccountPlatform.Guest);
|
|
if (response != null)
|
|
{
|
|
MyUser = new User
|
|
{
|
|
DisplayName = response.DisplayName,
|
|
Id = response.UserId,
|
|
OneTimeSocketToken = response.OneTimeToken,
|
|
WhichAccountPlatform = WhichAccountPlatform.Guest
|
|
};
|
|
await shogiSocket.OpenAsync(MyUser.Value.OneTimeSocketToken.ToString());
|
|
await localStorage.SetAccountPlatform(WhichAccountPlatform.Guest);
|
|
}
|
|
}
|
|
|
|
public async Task LoginWithMicrosoftAccount()
|
|
{
|
|
var state = await authState.GetAuthenticationStateAsync();
|
|
|
|
if (state.User?.Identity?.Name == null || state.User?.Identity?.IsAuthenticated == false)
|
|
{
|
|
// Set the login platform so that we know to log in with microsoft after being redirected away from the UI.
|
|
await localStorage.SetAccountPlatform(WhichAccountPlatform.Microsoft);
|
|
navigation.NavigateToLogin("authentication/login");
|
|
return;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Try to log in with the account used from the previous browser session.
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<bool> TryLoginSilentAsync()
|
|
{
|
|
var platform = await localStorage.GetAccountPlatform();
|
|
Console.WriteLine($"Try Login Silent - {platform}");
|
|
if (platform == WhichAccountPlatform.Guest)
|
|
{
|
|
var response = await shogiApi.GetToken(WhichAccountPlatform.Guest);
|
|
if (response != null)
|
|
{
|
|
MyUser = new User(
|
|
Id: response.UserId,
|
|
DisplayName: response.DisplayName,
|
|
WhichAccountPlatform: WhichAccountPlatform.Guest,
|
|
OneTimeSocketToken: response.OneTimeToken);
|
|
}
|
|
}
|
|
else if (platform == WhichAccountPlatform.Microsoft)
|
|
{
|
|
var state = await authState.GetAuthenticationStateAsync();
|
|
if (state.User?.Identity?.Name != null)
|
|
{
|
|
var response = await shogiApi.GetToken(WhichAccountPlatform.Microsoft);
|
|
if (response == null)
|
|
{
|
|
// Login failed, so reset local storage to avoid putting the user in a broken state.
|
|
await localStorage.DeleteAccountPlatform();
|
|
return false;
|
|
}
|
|
var id = state.User.Claims.Single(claim => claim.Type == "oid").Value;
|
|
var displayName = state.User.Identity.Name;
|
|
MyUser = new User(
|
|
Id: id,
|
|
DisplayName: displayName,
|
|
WhichAccountPlatform: WhichAccountPlatform.Microsoft,
|
|
OneTimeSocketToken: response.OneTimeToken);
|
|
}
|
|
}
|
|
|
|
if (MyUser != null)
|
|
{
|
|
await shogiSocket.OpenAsync(MyUser.Value.OneTimeSocketToken.ToString());
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public async Task LogoutAsync()
|
|
{
|
|
MyUser = null;
|
|
var platform = await localStorage.GetAccountPlatform();
|
|
await localStorage.DeleteAccountPlatform();
|
|
|
|
if (platform == WhichAccountPlatform.Guest)
|
|
{
|
|
await shogiApi.GuestLogout();
|
|
}
|
|
else if (platform == WhichAccountPlatform.Microsoft)
|
|
{
|
|
navigation.NavigateToLogout("authentication/logout");
|
|
}
|
|
}
|
|
}
|