This commit is contained in:
2023-01-28 13:21:47 -06:00
parent 11b387b928
commit 8a25c0ed35
26 changed files with 443 additions and 359 deletions

View File

@@ -3,7 +3,6 @@ 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;
@@ -35,21 +34,23 @@ public class AccountManager
this.shogiSocket = shogiSocket;
}
private User? MyUser { get => accountState.User; set => accountState.User = value; }
private User? MyUser => accountState.User;
private Task SetUser(User user) => accountState.SetUser(user);
public async Task LoginWithGuestAccount()
{
var response = await shogiApi.GetToken(WhichAccountPlatform.Guest);
if (response != null)
{
MyUser = new User
await SetUser(new User
{
DisplayName = response.DisplayName,
Id = response.UserId,
OneTimeSocketToken = response.OneTimeToken,
WhichAccountPlatform = WhichAccountPlatform.Guest
};
await shogiSocket.OpenAsync(MyUser.Value.OneTimeSocketToken.ToString());
});
await shogiSocket.OpenAsync(response.OneTimeToken.ToString());
await localStorage.SetAccountPlatform(WhichAccountPlatform.Guest);
}
}
@@ -80,11 +81,12 @@ public class AccountManager
var response = await shogiApi.GetToken(WhichAccountPlatform.Guest);
if (response != null)
{
MyUser = new User(
await accountState.SetUser(new User(
Id: response.UserId,
DisplayName: response.DisplayName,
WhichAccountPlatform: WhichAccountPlatform.Guest,
OneTimeSocketToken: response.OneTimeToken);
WhichAccountPlatform: WhichAccountPlatform.Guest));
await shogiSocket.OpenAsync(response.OneTimeToken.ToString());
return true;
}
}
else if (platform == WhichAccountPlatform.Microsoft)
@@ -101,26 +103,21 @@ public class AccountManager
}
var id = state.User.Claims.Single(claim => claim.Type == "oid").Value;
var displayName = state.User.Identity.Name;
MyUser = new User(
await accountState.SetUser(new User(
Id: id,
DisplayName: displayName,
WhichAccountPlatform: WhichAccountPlatform.Microsoft,
OneTimeSocketToken: response.OneTimeToken);
WhichAccountPlatform: WhichAccountPlatform.Microsoft));
await shogiSocket.OpenAsync(response.OneTimeToken.ToString());
return true;
}
}
if (MyUser != null)
{
await shogiSocket.OpenAsync(MyUser.Value.OneTimeSocketToken.ToString());
return true;
}
return false;
}
public async Task LogoutAsync()
{
MyUser = null;
await accountState.SetUser(null);
var platform = await localStorage.GetAccountPlatform();
await localStorage.DeleteAccountPlatform();

View File

@@ -1,28 +1,27 @@
namespace Shogi.UI.Pages.Home.Account;
using static Shogi.UI.Shared.Events;
namespace Shogi.UI.Pages.Home.Account;
public class AccountState
{
public event EventHandler<LoginEventArgs>? LoginChangedEvent;
public event AsyncEventHandler<LoginEventArgs>? LoginChangedEvent;
private User? user;
public User? User
public User? User { get; private set; }
public Task SetUser(User? user)
{
get => user;
set
User = user;
return EmitLoginChangedEvent();
}
private async Task EmitLoginChangedEvent()
{
if (LoginChangedEvent is not null)
{
if (user != value)
await LoginChangedEvent.Invoke(new LoginEventArgs
{
user = value;
EmitLoginChangedEvent();
}
User = User
});
}
}
private void EmitLoginChangedEvent()
{
LoginChangedEvent?.Invoke(this, new LoginEventArgs
{
User = User
});
}
}

View File

@@ -3,8 +3,7 @@
public readonly record struct User(
string Id,
string DisplayName,
WhichAccountPlatform WhichAccountPlatform,
Guid OneTimeSocketToken)
WhichAccountPlatform WhichAccountPlatform)
{
}
}