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; 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? MyUser { get => accountState.User; set => accountState.User = value; } public async Task LoginWithGuestAccount() { var response = await shogiApi.GetToken(); if (response != null) { 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); } } public async Task LoginWithMicrosoftAccount() { 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); //} } /// /// Try to log in with the account used from the previous browser session. /// /// public async Task TryLoginSilentAsync() { 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; } 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"); } } }