squash a bunch of commits

This commit is contained in:
2022-10-30 12:03:16 -05:00
parent 09b72c1858
commit 93027e8c57
222 changed files with 6157 additions and 3201 deletions

View File

@@ -0,0 +1,138 @@
using Microsoft.AspNetCore.Components;
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.GetGuestToken();
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()
{
throw new NotImplementedException();
//var state = await authState.GetAuthenticationStateAsync();
//if (state.User?.Identity?.Name == null || state.User?.Identity?.IsAuthenticated != true)
//{
// navigation.NavigateTo("authentication/login");
// return;
//}
//var id = state.User.Identity.Name;
//var socketToken = await shogiApi.GetToken();
//if (socketToken.HasValue)
//{
// User = new User
// {
// DisplayName = id,
// Id = id,
// OneTimeSocketToken = socketToken.Value
// };
// await ConnectToSocketAsync();
// 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.GetGuestToken();
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;
}
}

View File

@@ -0,0 +1,28 @@
namespace Shogi.UI.Pages.Home.Account;
public class AccountState
{
public event EventHandler<LoginEventArgs>? LoginChangedEvent;
private User? user;
public User? User
{
get => user;
set
{
if (user != value)
{
user = value;
EmitLoginChangedEvent();
}
}
}
private void EmitLoginChangedEvent()
{
LoginChangedEvent?.Invoke(this, new LoginEventArgs
{
User = User
});
}
}

View File

@@ -0,0 +1,24 @@
using Shogi.UI.Shared;
namespace Shogi.UI.Pages.Home.Account
{
public static class LocalStorageExtensions
{
private const string AccountPlatform = "AccountPlatform";
public static Task<WhichAccountPlatform?> GetAccountPlatform(this ILocalStorage self)
{
return self.Get<WhichAccountPlatform>(AccountPlatform).AsTask();
}
public static Task SetAccountPlatform(this ILocalStorage self, WhichAccountPlatform platform)
{
return self.Set(AccountPlatform, platform.ToString()).AsTask();
}
public static Task DeleteAccountPlatform(this ILocalStorage self)
{
return self.Delete(AccountPlatform).AsTask();
}
}
}

View File

@@ -0,0 +1,7 @@
namespace Shogi.UI.Pages.Home.Account
{
public class LoginEventArgs : EventArgs
{
public User? User { get; set; }
}
}

View File

@@ -0,0 +1,12 @@
namespace Shogi.UI.Pages.Home.Account
{
public class User
{
public string Id { get; set; }
public string DisplayName { get; set; }
public WhichAccountPlatform WhichAccountPlatform { get; set; }
public Guid OneTimeSocketToken { get; set; }
}
}

View File

@@ -0,0 +1,8 @@
namespace Shogi.UI.Pages.Home.Account
{
public enum WhichAccountPlatform
{
Guest,
Microsoft
}
}