feat: integrate d3-org-chart for network visualization and add SignalR token notification service
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Blazored.LocalStorage;
|
||||
using FrontOffice.BFF.User.Protobuf.Protos.User;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using MudBlazor;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
@@ -11,15 +12,25 @@ public class AuthService
|
||||
private readonly NavigationManager _navigation;
|
||||
private readonly ISnackbar _snackbar;
|
||||
private readonly UserAuthInfo _userAuthInfo;
|
||||
private readonly UserContract.UserContractClient _userContract;
|
||||
private readonly ILogger<AuthService> _logger;
|
||||
|
||||
private const string TokenStorageKey = "auth:token";
|
||||
|
||||
public AuthService(ILocalStorageService localStorage, NavigationManager navigation, ISnackbar snackbar, UserAuthInfo userAuthInfo)
|
||||
public AuthService(
|
||||
ILocalStorageService localStorage,
|
||||
NavigationManager navigation,
|
||||
ISnackbar snackbar,
|
||||
UserAuthInfo userAuthInfo,
|
||||
UserContract.UserContractClient userContract,
|
||||
ILogger<AuthService> logger)
|
||||
{
|
||||
_localStorage = localStorage;
|
||||
_navigation = navigation;
|
||||
_snackbar = snackbar;
|
||||
_userAuthInfo = userAuthInfo;
|
||||
_userContract = userContract;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<bool> IsAuthenticatedAsync()
|
||||
@@ -99,6 +110,51 @@ public class AuthService
|
||||
_navigation.NavigateTo(RouteConstants.Main.MainPage);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Refresh the user's token by calling the BFF RefreshToken API.
|
||||
/// If successful, the new token is stored and user info is updated.
|
||||
/// </summary>
|
||||
/// <returns>True if token was refreshed successfully</returns>
|
||||
public async Task<bool> RefreshTokenAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var currentToken = await GetTokenAsync();
|
||||
if (string.IsNullOrEmpty(currentToken))
|
||||
{
|
||||
_logger.LogWarning("No token found for refresh");
|
||||
return false;
|
||||
}
|
||||
|
||||
var response = await _userContract.RefreshTokenAsync(new RefreshTokenRequest
|
||||
{
|
||||
CurrentToken = currentToken
|
||||
});
|
||||
|
||||
if (response.Success && !string.IsNullOrEmpty(response.Token))
|
||||
{
|
||||
// Store the new token
|
||||
await _localStorage.SetItemAsync(TokenStorageKey, response.Token);
|
||||
|
||||
// Update user auth info with new token claims
|
||||
await InitUserAuthInfo();
|
||||
|
||||
_logger.LogInformation("Token refreshed successfully");
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation("Token refresh not needed: {Message}", response.Message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to refresh token");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task RequireAuthenticationAsync()
|
||||
{
|
||||
var isAuthenticated = await IsAuthenticatedAsync();
|
||||
|
||||
Reference in New Issue
Block a user