From dedcf0231cee9490880814804c874fead55db69d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Jais=C5=84ski?= Date: Fri, 8 May 2026 18:43:37 +0000 Subject: [PATCH] Add AuthenticationHeaderHandler to attach Bearer token in WASM client --- .../Handlers/AuthenticationHeaderHandler.cs | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/NexusReader.Web.Client/Handlers/AuthenticationHeaderHandler.cs 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); + } +}