Introduction of tags
This commit is contained in:
@@ -177,7 +177,54 @@ namespace LD_SysInfo.Services
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Sends the collected data to the server.
|
||||
/// Refreshes the current JWT token without requiring username/password.
|
||||
/// </summary>
|
||||
public async Task<bool> RefreshTokenAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(jwtToken))
|
||||
{
|
||||
Console.WriteLine("⚠️ No token to refresh.");
|
||||
return false;
|
||||
}
|
||||
|
||||
HttpResponseMessage response = await httpClient.PostAsync(BuildUrl("/api/auth/refresh"), null);
|
||||
string rawResponse = await response.Content.ReadAsStringAsync();
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
Console.WriteLine($"❌ Token refresh failed: {response.StatusCode} - {rawResponse}");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Parse the response to get the new token if provided in body
|
||||
// Note: Your server sets it via cookie, but may also return it in response
|
||||
try
|
||||
{
|
||||
var refreshResponse = JsonConvert.DeserializeObject<Dictionary<string, string>>(rawResponse);
|
||||
if (refreshResponse?.ContainsKey("token") == true)
|
||||
{
|
||||
SetJwtToken(refreshResponse["token"]);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Token might only be in cookie, which is fine
|
||||
}
|
||||
|
||||
Console.WriteLine("✅ Token refreshed successfully.");
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"❌ Exception during token refresh: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends the collected data to the server with automatic token refresh and re-auth.
|
||||
/// </summary>
|
||||
private async Task<HttpResponseMessage> SendWithAutoReauthAsync(Func<Task<HttpResponseMessage>> requestFunc)
|
||||
{
|
||||
@@ -185,8 +232,24 @@ namespace LD_SysInfo.Services
|
||||
|
||||
if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
|
||||
{
|
||||
Console.WriteLine("⚠️ Token expired or invalid. Attempting re-auth...");
|
||||
Console.WriteLine("⚠️ Token expired or invalid. Attempting token refresh...");
|
||||
|
||||
// Try refreshing the token first (faster than full re-auth)
|
||||
bool refreshed = await RefreshTokenAsync();
|
||||
|
||||
if (refreshed)
|
||||
{
|
||||
// Retry original request after refresh
|
||||
response = await requestFunc();
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
// If refresh failed or didn't work, fall back to full re-authentication
|
||||
Console.WriteLine("⚠️ Token refresh failed. Attempting full re-auth...");
|
||||
var loginResponse = await AuthenticateAsync(_config.Auth.Username, _config.Auth.Password);
|
||||
if (loginResponse == null || string.IsNullOrEmpty(loginResponse.Token))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user