Skip to content

Commit

Permalink
Prevent crashing when invalid token and can't be renewed
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkeye116477 committed Mar 7, 2024
1 parent 9415798 commit e92fff1
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/Services/EpicAccountClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,22 +153,29 @@ public async Task<bool> GetIsUserLoggedIn()
var account = await InvokeRequest<AccountResponse>(accountUrl + tokens.account_id, tokens);
return account.Item2.id == tokens.account_id;
}
catch (Exception e)
catch (Exception ex)
{
if (e is TokenException)
if (ex is TokenException)
{
await RenewTokens(tokens.refresh_token);
tokens = LoadTokens();
if (tokens is null)
var renewSuccess = await RenewTokens(tokens.refresh_token);
if (renewSuccess)
{
return false;
try
{
var account = await InvokeRequest<AccountResponse>(accountUrl + tokens.account_id, tokens);
return account.Item2.id == tokens.account_id;
}
catch (Exception ex2)
{
logger.Error(ex2, "Failed to validation Epic authentication.");
return false;
}
}
var account = await InvokeRequest<AccountResponse>(accountUrl + tokens.account_id, tokens);
return account.Item2.id == tokens.account_id;
return false;
}
else
{
logger.Error(e, "Failed to validation Epic authentication.");
logger.Error(ex, "Failed to validation Epic authentication.");
return false;
}
}
Expand Down Expand Up @@ -231,7 +238,7 @@ public CatalogItem GetCatalogItem(string nameSpace, string id, string cachePath)
}
}

private async Task RenewTokens(string refreshToken)
private async Task<bool> RenewTokens(string refreshToken)
{
using var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Clear();
Expand All @@ -245,10 +252,12 @@ private async Task RenewTokens(string refreshToken)
var respContent = await response.Content.ReadAsStringAsync();
FileSystem.CreateDirectory(Path.GetDirectoryName(tokensPath));
File.WriteAllText(tokensPath, respContent);
return true;
}
else
{
logger.Error("Failed to renew tokens.");
return false;
}
}

Expand Down

0 comments on commit e92fff1

Please sign in to comment.