Skip to content

Commit

Permalink
fix: Error setting multiple auth cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
Neo-Ciber94 committed Sep 26, 2023
1 parent f37fa37 commit eb0031a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
30 changes: 22 additions & 8 deletions packages/animelist-auth/src/common/cookieJar.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type CookieSerializeOptions, parse as parseCookie, serialize as serializeCookie } from "cookie";

type CookieValue = { value: string, options?: CookieSerializeOptions };
type CookieValue = { value: string, options?: CookieSerializeOptions, fromHeader?: boolean };

export class CookieJar {
private cookies = new Map<string, CookieValue>();
Expand All @@ -13,7 +13,7 @@ export class CookieJar {
const obj = parseCookie(cookieHeader);
for (const key in obj) {
const value = obj[key];
cookies.set(key, { value })
cookies.set(key, { value, fromHeader: true })
}

this.cookies = cookies;
Expand All @@ -32,11 +32,12 @@ export class CookieJar {
return this.cookies.get(name)?.value;
}

delete(name: string) {
delete(name: string, options?: CookieSerializeOptions) {
this.cookies.set(name, {
value: '',
options: {
expires: new Date(0)
...options,
maxAge: -1
}
});
}
Expand All @@ -49,11 +50,24 @@ export class CookieJar {
const serializedCookies: string[] = [];

for (const [name, cookieValue] of this.cookies.entries()) {
const { value, options } = cookieValue;
const serializedCookie = serializeCookie(name, value, options);
serializedCookies.push(serializedCookie);
const { value, options, fromHeader = false } = cookieValue;

if (!fromHeader) {
const serializedCookie = serializeCookie(name, value, options);
serializedCookies.push(serializedCookie);
}
}

return serializedCookies.join(", ");
}

toJSON() {
const obj: Record<string, string> = {};

for (const [name, cookiesValue] of this.cookies.entries()) {
obj[name] = cookiesValue.value;
}

return serializedCookies.join("; ");
return obj;
}
}
1 change: 1 addition & 0 deletions packages/animelist-auth/src/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

export * from "./getApiUrl";
export * from "./httpError";
export * from "./cookieJar";
export * from "./types";
export * from "./utils";
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Auth } from "../server";
import { HttpError, error, redirect } from "../../common/httpError";
import type { RequestEvent } from "../../common/types";
import type { HandleAuthOptions } from "./types";
import { CookieJar } from "src/common/cookieJar";
import { CookieJar } from "../../common/cookieJar";

const ALLOWED_FORWARD_HEADERS = [
"Authorization",
Expand Down

0 comments on commit eb0031a

Please sign in to comment.