Files
Shogi/Shogi.UI/Pages/Home/Account/AccountManager.cs

139 lines
3.9 KiB
C#

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Shogi.UI.Pages.Home.Api;
using Shogi.UI.Shared;
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? User { get => accountState.User; set => accountState.User = value; }
public async Task LoginWithGuestAccount()
{
var response = await shogiApi.GetToken();
if (response != null)
{
User = new User
{
DisplayName = response.DisplayName,
Id = response.UserId,
OneTimeSocketToken = response.OneTimeToken,
WhichAccountPlatform = WhichAccountPlatform.Guest
};
await shogiSocket.OpenAsync(User.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 != true)
{
navigation.NavigateTo("authentication/login");
return;
}
var response = await shogiApi.GetToken();
if (response != null)
{
User = new User
{
DisplayName = response.DisplayName,
Id = response.UserId,
OneTimeSocketToken = response.OneTimeToken,
WhichAccountPlatform = WhichAccountPlatform.Microsoft,
};
await shogiSocket.OpenAsync(User.OneTimeSocketToken.ToString());
await localStorage.SetAccountPlatform(WhichAccountPlatform.Microsoft);
}
}
/// <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();
if (platform == WhichAccountPlatform.Guest)
{
var response = await shogiApi.GetToken();
if (response != null)
{
User = new User
{
DisplayName = response.DisplayName,
Id = response.UserId,
OneTimeSocketToken = response.OneTimeToken,
WhichAccountPlatform = WhichAccountPlatform.Guest
};
}
}
else if (platform == WhichAccountPlatform.Microsoft)
{
Console.WriteLine("Login Microsoft");
throw new NotImplementedException();
//var state = await authState.GetAuthenticationStateAsync();
//if (state.User?.Identity?.Name != null)
//{
// var id = state.User.Identity;
// User = new User
// {
// DisplayName = id.Name,
// Id = id.Name
// };
// var token = await shogiApi.GetToken();
// if (token.HasValue)
// {
// User.OneTimeSocketToken = token.Value;
// }
//}
// TODO: If this fails then platform saved to localStorage should get cleared
}
if (User != null)
{
await shogiSocket.OpenAsync(User.OneTimeSocketToken.ToString());
return true;
}
return false;
}
public async Task LogoutAsync()
{
await Task.WhenAll(shogiApi.GuestLogout(), localStorage.DeleteAccountPlatform());
User = null;
}
}