Fix fire'n'forget task from blocking guest logins.

This commit is contained in:
Lucas Morgan
2023-07-07 16:46:26 -05:00
parent dacd0475f9
commit 8884d15d6e
4 changed files with 42 additions and 24 deletions

View File

@@ -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)
{