From 4ae39a9ff2a1d5d026f334e150e750d116ba5ba3 Mon Sep 17 00:00:00 2001 From: Leandro Hernan Rojas Date: Tue, 29 Apr 2025 08:59:34 -0300 Subject: [PATCH] Patch UI ProviderJWT --- .../CustomAuthorizationProvider.cs | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/phronCare.UIBlazor/Services/Authorization/CustomAuthorizationProvider.cs b/phronCare.UIBlazor/Services/Authorization/CustomAuthorizationProvider.cs index 115d95b..4becc9f 100644 --- a/phronCare.UIBlazor/Services/Authorization/CustomAuthorizationProvider.cs +++ b/phronCare.UIBlazor/Services/Authorization/CustomAuthorizationProvider.cs @@ -73,14 +73,26 @@ namespace phronCare.UIBlazor.Services.Authorization // }; //} - public async Task Login(string token) + //public async Task Login(string token) + //{ + // await js.RemoveItem(TOKENKEY); + // await js.SetInLocalStorage(TOKENKEY, token); + // var authState = BuildAuthenticationState(token); + // NotifyAuthenticationStateChanged(Task.FromResult(authState)); + //} + public async Task Login(string tokenJson) { + var tokenData = JsonSerializer.Deserialize(tokenJson); + + if (tokenData is null || string.IsNullOrWhiteSpace(tokenData.token)) + throw new Exception("Token inválido o mal formado."); + await js.RemoveItem(TOKENKEY); - await js.SetInLocalStorage(TOKENKEY, token); - var authState = BuildAuthenticationState(token); + await js.SetInLocalStorage(TOKENKEY, tokenData.token); // ✔️ Guardamos solo el JWT string + + var authState = BuildAuthenticationState(tokenData.token); NotifyAuthenticationStateChanged(Task.FromResult(authState)); } - public async Task Logout() { httpClient.DefaultRequestHeaders.Authorization = null;