From 693e60123931166bb9e1dbbb8e8c9a339b2eab4b Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Tue, 16 Jul 2024 03:01:18 +0100 Subject: [PATCH] Removed condition that we need to have a successful response to change cookies --- .../Handlers/ClientCookieSessionHandler.cs | 49 +++++++++---------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/src/PinguApps.Appwrite.Client/Handlers/ClientCookieSessionHandler.cs b/src/PinguApps.Appwrite.Client/Handlers/ClientCookieSessionHandler.cs index ba52b591..87c18111 100644 --- a/src/PinguApps.Appwrite.Client/Handlers/ClientCookieSessionHandler.cs +++ b/src/PinguApps.Appwrite.Client/Handlers/ClientCookieSessionHandler.cs @@ -30,40 +30,37 @@ protected override async Task SendAsync(HttpRequestMessage private void SaveSession(HttpResponseMessage response) { - if (response.IsSuccessStatusCode) + if (response.Headers.TryGetValues("Set-Cookie", out var values)) { - if (response.Headers.TryGetValues("Set-Cookie", out var values)) - { - var sessionCookie = values.FirstOrDefault(x => x.StartsWith("a_session", StringComparison.OrdinalIgnoreCase) && !x.Contains("legacy", StringComparison.OrdinalIgnoreCase)); + var sessionCookie = values.FirstOrDefault(x => x.StartsWith("a_session", StringComparison.OrdinalIgnoreCase) && !x.Contains("legacy", StringComparison.OrdinalIgnoreCase)); - if (sessionCookie is null) - return; + if (sessionCookie is null) + return; - var afterEquals = sessionCookie.IndexOf('=') + 1; - var semicolonIndex = sessionCookie.IndexOf(';', afterEquals); - var base64 = sessionCookie.Substring(afterEquals, semicolonIndex - afterEquals); + var afterEquals = sessionCookie.IndexOf('=') + 1; + var semicolonIndex = sessionCookie.IndexOf(';', afterEquals); + var base64 = sessionCookie.Substring(afterEquals, semicolonIndex - afterEquals); - if (string.Equals(base64, "deleted", StringComparison.OrdinalIgnoreCase)) - { - AppwriteClient.SetSession(null); - return; - } + if (string.Equals(base64, "deleted", StringComparison.OrdinalIgnoreCase)) + { + AppwriteClient.SetSession(null); + return; + } - var decodedBytes = Convert.FromBase64String(base64); - var decoded = Encoding.UTF8.GetString(decodedBytes); + var decodedBytes = Convert.FromBase64String(base64); + var decoded = Encoding.UTF8.GetString(decodedBytes); - try - { - var sessionData = JsonSerializer.Deserialize(decoded); + try + { + var sessionData = JsonSerializer.Deserialize(decoded); - if (sessionData is null || sessionData.Id is null || sessionData.Secret is null) - return; + if (sessionData is null || sessionData.Id is null || sessionData.Secret is null) + return; - AppwriteClient.SetSession(sessionData.Secret); - } - catch (JsonException) - { - } + AppwriteClient.SetSession(sessionData.Secret); + } + catch (JsonException) + { } } }