diff --git a/src/Backend/DiunaBI.UI.Shared/Extensions/ServiceCollectionExtensions.cs b/src/Backend/DiunaBI.UI.Shared/Extensions/ServiceCollectionExtensions.cs index 7e59490..4c28233 100644 --- a/src/Backend/DiunaBI.UI.Shared/Extensions/ServiceCollectionExtensions.cs +++ b/src/Backend/DiunaBI.UI.Shared/Extensions/ServiceCollectionExtensions.cs @@ -1,5 +1,6 @@ using Microsoft.Extensions.DependencyInjection; using DiunaBI.UI.Shared.Services; +using DiunaBI.UI.Shared.Handlers; namespace DiunaBI.UI.Shared.Extensions; @@ -7,10 +8,23 @@ public static class ServiceCollectionExtensions { public static IServiceCollection AddSharedServices(this IServiceCollection services, string apiBaseUrl) { - // HttpClient for API calls + // HttpClient for API calls with logging // Ensure BaseAddress ends with / for proper relative URL resolution var baseUri = apiBaseUrl.EndsWith('/') ? apiBaseUrl : apiBaseUrl + "/"; - services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(baseUri) }); + + services.AddTransient(); + + services.AddHttpClient(client => + { + client.BaseAddress = new Uri(baseUri); + }) + .AddHttpMessageHandler(); + + services.AddHttpClient(client => + { + client.BaseAddress = new Uri(baseUri); + }) + .AddHttpMessageHandler(); // Services services.AddScoped(); diff --git a/src/Backend/DiunaBI.UI.Shared/Handlers/HttpLoggingHandler.cs b/src/Backend/DiunaBI.UI.Shared/Handlers/HttpLoggingHandler.cs new file mode 100644 index 0000000..57b1c3b --- /dev/null +++ b/src/Backend/DiunaBI.UI.Shared/Handlers/HttpLoggingHandler.cs @@ -0,0 +1,28 @@ +namespace DiunaBI.UI.Shared.Handlers; + +public class HttpLoggingHandler : DelegatingHandler +{ + protected override async Task SendAsync( + HttpRequestMessage request, + CancellationToken cancellationToken) + { + Console.WriteLine($"🌐 HTTP Request: {request.Method} {request.RequestUri}"); + Console.WriteLine($" BaseAddress: {request.RequestUri?.GetLeftPart(UriPartial.Authority)}"); + Console.WriteLine($" Path: {request.RequestUri?.PathAndQuery}"); + + if (request.Headers.Authorization != null) + { + Console.WriteLine($" Auth: {request.Headers.Authorization.Scheme} {request.Headers.Authorization.Parameter?[..Math.Min(20, request.Headers.Authorization.Parameter?.Length ?? 0)]}..."); + } + else + { + Console.WriteLine($" Auth: None"); + } + + var response = await base.SendAsync(request, cancellationToken); + + Console.WriteLine($" Response: {(int)response.StatusCode} {response.StatusCode}"); + + return response; + } +} diff --git a/src/Backend/DiunaBI.UI.Shared/Services/AuthService.cs b/src/Backend/DiunaBI.UI.Shared/Services/AuthService.cs index 42b109e..f467767 100644 --- a/src/Backend/DiunaBI.UI.Shared/Services/AuthService.cs +++ b/src/Backend/DiunaBI.UI.Shared/Services/AuthService.cs @@ -36,7 +36,6 @@ public class AuthService { Console.WriteLine($"=== ValidateWithBackend: Sending Google credential for {email} ==="); - // Wyślij Google credential do backendu var response = await _httpClient.PostAsJsonAsync("Auth/apiToken", googleCredential); if (response.IsSuccessStatusCode) @@ -53,11 +52,9 @@ public class AuthService AvatarUrl = avatarUrl }; - // Zapisz do localStorage await _jsRuntime.InvokeVoidAsync("localStorage.setItem", "api_token", _apiToken); await _jsRuntime.InvokeVoidAsync("localStorage.setItem", "user_info", JsonSerializer.Serialize(_userInfo)); - // Ustaw header dla przyszłych requestów _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _apiToken);