Fix fire'n'forget task from blocking guest logins.
This commit is contained in:
@@ -45,8 +45,13 @@ public class AccountManager
|
||||
Id = response.UserId,
|
||||
WhichAccountPlatform = WhichAccountPlatform.Guest
|
||||
});
|
||||
await shogiSocket.OpenAsync(response.OneTimeToken.ToString());
|
||||
await localStorage.SetAccountPlatform(WhichAccountPlatform.Guest);
|
||||
// TODO: OpenAsync() sometimes doesn't return, probably because of the fire'n'forget task inside it. Figure that out.
|
||||
await shogiSocket.OpenAsync(response.OneTimeToken.ToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidOperationException("Failed to get token from server during guest login.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,16 +119,19 @@ public class AccountManager
|
||||
{
|
||||
var platform = await localStorage.GetAccountPlatform();
|
||||
await localStorage.DeleteAccountPlatform();
|
||||
await accountState.SetUser(null);
|
||||
|
||||
if (platform == WhichAccountPlatform.Guest)
|
||||
{
|
||||
await shogiApi.GuestLogout();
|
||||
await accountState.SetUser(null);
|
||||
}
|
||||
else if (platform == WhichAccountPlatform.Microsoft)
|
||||
{
|
||||
await accountState.SetUser(null);
|
||||
navigation.NavigateToLogout("authentication/logout");
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidOperationException("Tried to logout without a valid account platform.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,29 +11,32 @@ namespace Shogi.UI.Pages.Home.Api
|
||||
{
|
||||
public const string GuestClientName = "Guest";
|
||||
public const string MsalClientName = "Msal";
|
||||
//public const string AnonymousClientName = "Anonymous";
|
||||
|
||||
private readonly JsonSerializerOptions serializerOptions;
|
||||
private readonly IHttpClientFactory clientFactory;
|
||||
private readonly AccountState accountState;
|
||||
private readonly HttpClient guestHttpClient;
|
||||
private readonly HttpClient msalHttpClient;
|
||||
|
||||
public ShogiApi(IHttpClientFactory clientFactory, AccountState accountState)
|
||||
{
|
||||
serializerOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web);
|
||||
this.clientFactory = clientFactory;
|
||||
this.accountState = accountState;
|
||||
this.guestHttpClient = clientFactory.CreateClient(GuestClientName);
|
||||
this.msalHttpClient = clientFactory.CreateClient(MsalClientName);
|
||||
}
|
||||
|
||||
private HttpClient HttpClient => accountState.User?.WhichAccountPlatform switch
|
||||
{
|
||||
WhichAccountPlatform.Guest => clientFactory.CreateClient(GuestClientName),
|
||||
WhichAccountPlatform.Microsoft => clientFactory.CreateClient(MsalClientName),
|
||||
WhichAccountPlatform.Guest => this.guestHttpClient,
|
||||
WhichAccountPlatform.Microsoft => this.msalHttpClient,
|
||||
_ => throw new InvalidOperationException("AccountState.User must not be null during API call.")
|
||||
};
|
||||
|
||||
public async Task GuestLogout()
|
||||
{
|
||||
var response = await HttpClient.PutAsync(new Uri("User/GuestLogout", UriKind.Relative), null);
|
||||
var response = await this.guestHttpClient.PutAsync(new Uri("User/GuestLogout", UriKind.Relative), null);
|
||||
response.EnsureSuccessStatusCode();
|
||||
}
|
||||
|
||||
@@ -63,8 +66,8 @@ namespace Shogi.UI.Pages.Home.Api
|
||||
public async Task<CreateTokenResponse?> GetToken(WhichAccountPlatform whichAccountPlatform)
|
||||
{
|
||||
var httpClient = whichAccountPlatform == WhichAccountPlatform.Microsoft
|
||||
? clientFactory.CreateClient(MsalClientName)
|
||||
: clientFactory.CreateClient(GuestClientName);
|
||||
? this.msalHttpClient
|
||||
: this.guestHttpClient;
|
||||
var response = await httpClient.GetAsync(RelativeUri("User/Token"));
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user