Skip to content

Commit

Permalink
fix ux issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmoradi committed Nov 12, 2024
1 parent 84d04d4 commit 2d61e6f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,16 @@ public override async Task<AuthenticationState> GetAuthenticationStateAsync()
try
{
var access_token = await prerenderStateService.GetValue(() => tokenProvider.GetAccessToken());
var claimsPrinciple = tokenProvider.ParseAccessToken(access_token);

bool inPrerenderSession = AppPlatform.IsBlazorHybrid is false && jsRuntime.IsInitialized() is false;

if (claimsPrinciple.IsAuthenticated() is false && inPrerenderSession is false)
if (string.IsNullOrEmpty(access_token) && inPrerenderSession is false)
{
try
{
await semaphore.WaitAsync();
claimsPrinciple = tokenProvider.ParseAccessToken(await tokenProvider.GetAccessToken());
if (claimsPrinciple.IsAuthenticated() is false) // Check again after acquiring the lock.
access_token = await tokenProvider.GetAccessToken();
if (string.IsNullOrEmpty(access_token)) // Check again after acquiring the lock.
{
string? refresh_token = await storageService.GetItem("refresh_token");

Expand All @@ -103,7 +102,7 @@ public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
var refreshTokenResponse = await identityController.Refresh(new() { RefreshToken = refresh_token }, CancellationToken.None);
await StoreTokens(refreshTokenResponse!);
claimsPrinciple = tokenProvider.ParseAccessToken(refreshTokenResponse!.AccessToken);
access_token = refreshTokenResponse!.AccessToken;
}
catch (UnauthorizedException) // refresh_token is either invalid or expired.
{
Expand All @@ -118,7 +117,7 @@ public override async Task<AuthenticationState> GetAuthenticationStateAsync()
}
}

return new AuthenticationState(claimsPrinciple);
return new AuthenticationState(tokenProvider.ParseAccessToken(access_token, validateExpiry: false /* For better UX in order to minimize Routes.razor's Authorizing loading duration. */));
}
catch (Exception exp)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ public interface IAuthTokenProvider

public ClaimsPrincipal Anonymous() => new(new ClaimsIdentity());

public ClaimsPrincipal ParseAccessToken(string? access_token)
public ClaimsPrincipal ParseAccessToken(string? access_token, bool validateExpiry)
{
if (string.IsNullOrEmpty(access_token) is true)
return Anonymous();

var claims = ReadClaims(access_token);
var claims = ReadClaims(access_token, validateExpiry);

if (claims is null)
return Anonymous();
Expand All @@ -25,11 +25,11 @@ public ClaimsPrincipal ParseAccessToken(string? access_token)
return claimPrinciple;
}

private IEnumerable<Claim>? ReadClaims(string access_token)
private IEnumerable<Claim>? ReadClaims(string access_token, bool validateExpiry)
{
var parsedClaims = DeserializeAccessToken(access_token);

if (long.TryParse(parsedClaims["exp"].ToString(), out var expSeconds))
if (validateExpiry && long.TryParse(parsedClaims["exp"].ToString(), out var expSeconds))
{
var expirationDate = DateTimeOffset.FromUnixTimeSeconds(expSeconds);
if (expirationDate <= DateTimeOffset.UtcNow)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
var access_token = await tokenProvider.GetAccessToken();
if (access_token is not null)
{
if (tokenProvider.ParseAccessToken(access_token).IsAuthenticated() is false)
if (tokenProvider.ParseAccessToken(access_token, validateExpiry: true).IsAuthenticated() is false)
throw new UnauthorizedException();

request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", access_token);
Expand Down

0 comments on commit 2d61e6f

Please sign in to comment.