diff --git a/src/NexusReader.Web.Client/Handlers/AuthenticationHeaderHandler.cs b/src/NexusReader.Web.Client/Handlers/AuthenticationHeaderHandler.cs new file mode 100644 index 0000000..3772810 --- /dev/null +++ b/src/NexusReader.Web.Client/Handlers/AuthenticationHeaderHandler.cs @@ -0,0 +1,27 @@ +using System.Net.Http.Headers; +using NexusReader.Application.Abstractions.Services; + +namespace NexusReader.Web.Client.Handlers; + +public class AuthenticationHeaderHandler : DelegatingHandler +{ + private readonly INativeStorageService _storageService; + private const string TokenKey = "nexus_auth_token"; + + public AuthenticationHeaderHandler(INativeStorageService storageService) + { + _storageService = storageService; + } + + protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + var tokenResult = await _storageService.GetSecureString(TokenKey); + + if (tokenResult.IsSuccess && !string.IsNullOrEmpty(tokenResult.Value)) + { + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokenResult.Value); + } + + return await base.SendAsync(request, cancellationToken); + } +}