36 lines
1.2 KiB
C#
36 lines
1.2 KiB
C#
using DiunaBI.UI.Shared.Services;
|
|
|
|
namespace DiunaBI.UI.Shared.Handlers;
|
|
|
|
public class AuthenticationHandler : DelegatingHandler
|
|
{
|
|
private readonly TokenProvider _tokenProvider;
|
|
|
|
public AuthenticationHandler(TokenProvider tokenProvider)
|
|
{
|
|
_tokenProvider = tokenProvider;
|
|
}
|
|
|
|
protected override async Task<HttpResponseMessage> SendAsync(
|
|
HttpRequestMessage request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
// Get token from TokenProvider
|
|
var token = _tokenProvider.Token;
|
|
|
|
Console.WriteLine($"🔐 AuthenticationHandler: Token = {(string.IsNullOrEmpty(token) ? "NULL" : $"{token[..Math.Min(20, token.Length)]}...")}");
|
|
|
|
if (!string.IsNullOrEmpty(token))
|
|
{
|
|
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
|
|
Console.WriteLine($"🔐 AuthenticationHandler: Added Bearer token to request");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine($"🔐 AuthenticationHandler: No token available, request will be unauthorized");
|
|
}
|
|
|
|
return await base.SendAsync(request, cancellationToken);
|
|
}
|
|
}
|