Skip to content

Commit

Permalink
wip: fix cookies magento
Browse files Browse the repository at this point in the history
  • Loading branch information
IncognitaDev committed Dec 17, 2024
1 parent 0d52e33 commit b968ddb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 24 deletions.
45 changes: 25 additions & 20 deletions magento/actions/cart/addItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { handleCartActions } from "../../utils/cart.ts";
import { FORM_KEY_COOKIE } from "../../utils/constants.ts";
import { HttpError } from "../../../utils/http.ts";
import { OverrideFeatures } from "../../utils/client/types.ts";
import { proxySetCookie } from "../../utils/utils.ts";

export interface Props extends OverrideFeatures {
qty: number;
Expand Down Expand Up @@ -69,26 +70,30 @@ const action = async (

let cartId;
const cookies = fetchHeaders.getSetCookie();
cookies.forEach((cookie) => {
const parsed = parseCookieString(cookie, url.includes("localhost"));

if (parsed.name === "dataservices_cart_id") {
cartId = parsed.value.replace(/%22/g, "");
setCookie(ctx.response.headers, {
...parsed,
httpOnly: true,
secure: true,
path: "/",
unparsed: ["Priority=High"],
});
return;
}

setCookie(ctx.response.headers, {
...parsed,
path: "/",
});
});

proxySetCookie(fetchHeaders, ctx.response.headers, new URL(url).origin);

console.log({ cookies });
// cookies.forEach((cookie) => {
// const parsed = parseCookieString(cookie, url.includes("localhost"));

// if (parsed.name === "dataservices_cart_id") {
// cartId = parsed.value.replace(/%22/g, "");
// setCookie(ctx.response.headers, {
// ...parsed,
// httpOnly: true,
// secure: true,
// path: "/",
// unparsed: ["Priority=High"],
// });
// return;
// }

// setCookie(ctx.response.headers, {
// ...parsed,
// path: "/",
// });
// });

return handleCartActions(dontReturnCart, {
req,
Expand Down
27 changes: 23 additions & 4 deletions magento/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ export const proxySetCookie = (
to: Headers,
toDomain?: URL | string,
) => {
const newDomain = toDomain && new URL(toDomain);
const domain = toDomain && new URL(toDomain);
const newDomain = domain && !domain.hostname.includes("localhost")
? new URL(getTopDomain(domain.hostname))
: domain;

console.log({ newDomain });

for (const cookie of getSetCookies(from)) {
if (cookie.name === SESSION_COOKIE) {
continue;
}
// if (cookie.name === SESSION_COOKIE) {
// continue;
// }
const newCookie = newDomain
? {
...cookie,
Expand Down Expand Up @@ -44,3 +49,17 @@ export const sortSearchParams = (url: URL) => {
};

export const sanitizePath = (path: string) => path.replace(/^\/?(rest\/)?/, "");

export const getTopDomain = (domain: string): string => {
// Remove protocol and any trailing slash
const cleanDomain = domain.replace(/^(https?:)?\/\//, "").replace(/\/$/, "");

// Split by dots and get the last two parts
const parts = cleanDomain.split(".");
if (parts.length <= 2) return cleanDomain;
if (parts.length > 3) {
return parts.slice(-3).join(".");
}

return parts.slice(-2).join(".");
};

0 comments on commit b968ddb

Please sign in to comment.