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) + { } } }