Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Prevent Cleared event being raised when there aren't any tokens #746

Merged
merged 1 commit into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Uno.Extensions.Authentication/AuthenticationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public async ValueTask<bool> LogoutAsync(IDispatcher? dispatcher, CancellationTo

if (_logger.IsEnabled(LogLevel.Trace)) _logger.LogTraceMessage($"Logout successful, so clear token cache");
await _tokens.ClearAsync(ct);

// Don't raise LoggedOut event here - if there were tokens, then the ITokenCache.Cleared event will
// be raised, which in turn will trigger the LoggedOut event to be raised
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
{
if (_logger.IsEnabled(LogLevel.Debug)) _logger.LogDebugMessage($"The request '{request.RequestUri}' was unauthorized and the tokens cannot be refreshed. Considering the session has expired.");

// Request was unauthorized and we cannot refresh the authentication token.
await _tokens.ClearAsync(ct);
// Request was unauthorized and we cannot refresh the authentication token.
await _tokens.ClearAsync(ct);

return response;
}
Expand All @@ -104,8 +104,8 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
{
if (_logger.IsEnabled(LogLevel.Debug)) _logger.LogDebugMessage($"The request '{request.RequestUri}' was unauthorized and the token failed to refresh. Considering the session has expired.");

// No authentication token to use.
await _tokens.ClearAsync(ct);
// No authentication token to use.
await _tokens.ClearAsync(ct);

return response;
}
Expand Down Expand Up @@ -159,7 +159,7 @@ protected virtual async Task<bool> RefreshAuthenticationToken(CancellationToken
{
return await _authenticationService.RefreshAsync(ct);
}
catch(Exception ex)
catch (Exception ex)
{
if (_logger.IsEnabled(LogLevel.Debug)) _logger.LogDebugMessage($"Error refreshing token - {ex.Message}");
return false;
Expand Down
22 changes: 14 additions & 8 deletions src/Uno.Extensions.Authentication/TokenCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,23 @@ public TokenCache(
public async ValueTask ClearAsync(CancellationToken cancellation)
{
if (_logger.IsEnabled(LogLevel.Trace)) _logger.LogTraceMessage("Clearing tokens by invoking SaveAsync with empty dictionary");
// Don't acquire lock since this is done in the Save method
// Don't acquire lock since this is done in the Get/Save methods respectively
var existingTokens = await GetAsync(cancellation);
await SaveAsync(string.Empty, new Dictionary<string, string>(), cancellation);
if (_logger.IsEnabled(LogLevel.Trace)) _logger.LogTraceMessage("Tokens cleared");
try
{
if (_logger.IsEnabled(LogLevel.Trace)) _logger.LogTraceMessage("Raising Cleared event");
Cleared?.Invoke(this, EventArgs.Empty);
}
catch (Exception ex)
if (existingTokens.Any())
{
if (_logger.IsEnabled(LogLevel.Error)) _logger.LogErrorMessage($"Error raising Cleared event - check listeners to fix errors handling this event {ex.Message}");
// Only triggered cleared event if there were actually tokens to be cleared
// This prevents Cleared being raised when the user isn't logged in
try
{
if (_logger.IsEnabled(LogLevel.Trace)) _logger.LogTraceMessage("Raising Cleared event");
Cleared?.Invoke(this, EventArgs.Empty);
}
catch (Exception ex)
{
if (_logger.IsEnabled(LogLevel.Error)) _logger.LogErrorMessage($"Error raising Cleared event - check listeners to fix errors handling this event {ex.Message}");
}
}
}

Expand Down