squash a bunch of commits
This commit is contained in:
138
Shogi.UI/Pages/Home/Account/AccountManager.cs
Normal file
138
Shogi.UI/Pages/Home/Account/AccountManager.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
28
Shogi.UI/Pages/Home/Account/AccountState.cs
Normal file
28
Shogi.UI/Pages/Home/Account/AccountState.cs
Normal 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
|
||||
});
|
||||
}
|
||||
}
|
||||
24
Shogi.UI/Pages/Home/Account/LocalStorageExtensions.cs
Normal file
24
Shogi.UI/Pages/Home/Account/LocalStorageExtensions.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
7
Shogi.UI/Pages/Home/Account/LoginEventArgs.cs
Normal file
7
Shogi.UI/Pages/Home/Account/LoginEventArgs.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Shogi.UI.Pages.Home.Account
|
||||
{
|
||||
public class LoginEventArgs : EventArgs
|
||||
{
|
||||
public User? User { get; set; }
|
||||
}
|
||||
}
|
||||
12
Shogi.UI/Pages/Home/Account/User.cs
Normal file
12
Shogi.UI/Pages/Home/Account/User.cs
Normal 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; }
|
||||
}
|
||||
}
|
||||
8
Shogi.UI/Pages/Home/Account/WhichAccountPlatform.cs
Normal file
8
Shogi.UI/Pages/Home/Account/WhichAccountPlatform.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace Shogi.UI.Pages.Home.Account
|
||||
{
|
||||
public enum WhichAccountPlatform
|
||||
{
|
||||
Guest,
|
||||
Microsoft
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user