Skip to content

Commit

Permalink
Merge pull request #74 from PinguApps/73-session-handler-unsuccessful…
Browse files Browse the repository at this point in the history
…-responses

Removed condition that we need to have a successful response to change cookies
  • Loading branch information
pingu2k4 authored Jul 16, 2024
2 parents 7848520 + 693e601 commit 490e315
Showing 1 changed file with 23 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,40 +30,37 @@ protected override async Task<HttpResponseMessage> 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<CookieSessionData>(decoded);
try
{
var sessionData = JsonSerializer.Deserialize<CookieSessionData>(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)
{
}
}
}
Expand Down

0 comments on commit 490e315

Please sign in to comment.