reintroduce microsoft login. upgrade a bunch of stuff.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
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;
|
||||
|
||||
@@ -7,132 +8,144 @@ 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;
|
||||
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)
|
||||
public AccountManager(
|
||||
AccountState accountState,
|
||||
IShogiApi unauthenticatedClient,
|
||||
IConfiguration configuration,
|
||||
AuthenticationStateProvider authState,
|
||||
ILocalStorage localStorage,
|
||||
NavigationManager navigation,
|
||||
ShogiSocket shogiSocket)
|
||||
{
|
||||
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;
|
||||
this.accountState = accountState;
|
||||
this.shogiApi = unauthenticatedClient;
|
||||
this.configuration = configuration;
|
||||
this.authState = authState;
|
||||
this.localStorage = localStorage;
|
||||
this.navigation = navigation;
|
||||
this.shogiSocket = shogiSocket;
|
||||
}
|
||||
|
||||
var response = await shogiApi.GetToken();
|
||||
if (response != null)
|
||||
{
|
||||
User = new User
|
||||
{
|
||||
DisplayName = response.DisplayName,
|
||||
Id = response.UserId,
|
||||
OneTimeSocketToken = response.OneTimeToken,
|
||||
WhichAccountPlatform = WhichAccountPlatform.Microsoft,
|
||||
};
|
||||
private User? MyUser { get => accountState.User; set => accountState.User = value; }
|
||||
|
||||
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)
|
||||
public async Task LoginWithGuestAccount()
|
||||
{
|
||||
var response = await shogiApi.GetToken();
|
||||
if (response != null)
|
||||
{
|
||||
User = new User
|
||||
var response = await shogiApi.GetToken();
|
||||
if (response != null)
|
||||
{
|
||||
DisplayName = response.DisplayName,
|
||||
Id = response.UserId,
|
||||
OneTimeSocketToken = response.OneTimeToken,
|
||||
WhichAccountPlatform = WhichAccountPlatform.Guest
|
||||
};
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
else if (platform == WhichAccountPlatform.Microsoft)
|
||||
|
||||
public async Task LoginWithMicrosoftAccount()
|
||||
{
|
||||
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
|
||||
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;
|
||||
}
|
||||
|
||||
//var response = await shogiApi.GetToken();
|
||||
//if (response != null)
|
||||
//{
|
||||
// MyUser = new User
|
||||
// {
|
||||
// DisplayName = response.DisplayName,
|
||||
// Id = response.UserId,
|
||||
// OneTimeSocketToken = response.OneTimeToken,
|
||||
// WhichAccountPlatform = WhichAccountPlatform.Microsoft,
|
||||
// };
|
||||
|
||||
// await shogiSocket.OpenAsync(MyUser.Value.OneTimeSocketToken.ToString());
|
||||
// await localStorage.SetAccountPlatform(WhichAccountPlatform.Microsoft);
|
||||
//}
|
||||
}
|
||||
|
||||
if (User != null)
|
||||
/// <summary>
|
||||
/// Try to log in with the account used from the previous browser session.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> TryLoginSilentAsync()
|
||||
{
|
||||
await shogiSocket.OpenAsync(User.OneTimeSocketToken.ToString());
|
||||
return true;
|
||||
var platform = await localStorage.GetAccountPlatform();
|
||||
Console.WriteLine($"Try Login Silent - {platform}");
|
||||
if (platform == WhichAccountPlatform.Guest)
|
||||
{
|
||||
var response = await shogiApi.GetToken();
|
||||
if (response != null)
|
||||
{
|
||||
MyUser = new User
|
||||
{
|
||||
DisplayName = response.DisplayName,
|
||||
Id = response.UserId,
|
||||
OneTimeSocketToken = response.OneTimeToken,
|
||||
WhichAccountPlatform = WhichAccountPlatform.Guest
|
||||
};
|
||||
}
|
||||
}
|
||||
else if (platform == WhichAccountPlatform.Microsoft)
|
||||
{
|
||||
var state = await authState.GetAuthenticationStateAsync();
|
||||
if (state.User?.Identity?.Name != null)
|
||||
{
|
||||
var response = await shogiApi.GetToken();
|
||||
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.Identity;
|
||||
MyUser = new User
|
||||
{
|
||||
DisplayName = id.Name,
|
||||
Id = id.Name,
|
||||
OneTimeSocketToken = response.OneTimeToken
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (MyUser != null)
|
||||
{
|
||||
await shogiSocket.OpenAsync(MyUser.Value.OneTimeSocketToken.ToString());
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public async Task LogoutAsync()
|
||||
{
|
||||
await Task.WhenAll(shogiApi.GuestLogout(), localStorage.DeleteAccountPlatform());
|
||||
User = null;
|
||||
}
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,27 +2,27 @@
|
||||
|
||||
public class AccountState
|
||||
{
|
||||
public event EventHandler<LoginEventArgs>? LoginChangedEvent;
|
||||
public event EventHandler<LoginEventArgs>? LoginChangedEvent;
|
||||
|
||||
private User? user;
|
||||
public User? User
|
||||
{
|
||||
get => user;
|
||||
set
|
||||
{
|
||||
if (user != value)
|
||||
{
|
||||
user = value;
|
||||
EmitLoginChangedEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
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
|
||||
});
|
||||
}
|
||||
private void EmitLoginChangedEvent()
|
||||
{
|
||||
LoginChangedEvent?.Invoke(this, new LoginEventArgs
|
||||
{
|
||||
User = User
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
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; }
|
||||
}
|
||||
public readonly record struct User(
|
||||
string Id,
|
||||
string DisplayName,
|
||||
WhichAccountPlatform WhichAccountPlatform,
|
||||
Guid OneTimeSocketToken)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user