From 0b50c03e44520569044275b1300be61f04740246 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Tue, 6 Jan 2026 10:11:43 -0500 Subject: [PATCH] feat(client): add AuthRedirectHandler for global 401 redirect Add HTTP message handler that intercepts 401 Unauthorized responses and redirects to the login page with return URL preserved. --- .../Http/AuthRedirectHandler.cs | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 NEW/src/JdeScoping.Client/Http/AuthRedirectHandler.cs diff --git a/NEW/src/JdeScoping.Client/Http/AuthRedirectHandler.cs b/NEW/src/JdeScoping.Client/Http/AuthRedirectHandler.cs new file mode 100644 index 0000000..2731dad --- /dev/null +++ b/NEW/src/JdeScoping.Client/Http/AuthRedirectHandler.cs @@ -0,0 +1,33 @@ +using System.Net; +using Microsoft.AspNetCore.Components; + +namespace JdeScoping.Client.Http; + +/// +/// HTTP message handler that intercepts 401 Unauthorized responses +/// and redirects to the login page with return URL. +/// +public class AuthRedirectHandler : DelegatingHandler +{ + private readonly NavigationManager _navigationManager; + + public AuthRedirectHandler(NavigationManager navigationManager) + { + _navigationManager = navigationManager; + } + + protected override async Task SendAsync( + HttpRequestMessage request, + CancellationToken cancellationToken) + { + var response = await base.SendAsync(request, cancellationToken); + + if (response.StatusCode == HttpStatusCode.Unauthorized) + { + var returnUrl = Uri.EscapeDataString(_navigationManager.Uri); + _navigationManager.NavigateTo($"/login?returnUrl={returnUrl}", forceLoad: true); + } + + return response; + } +}