Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions vtex/loaders/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ const logMismatchedCart = (cart: OrderForm, req: Request, ctx: AppContext) => {
const userFromCookie = cookies[`VtexIdclientAutCookie_${ctx.account}`];

if (!userFromCookie) {
return;
return { shouldClearCartCookie: false };
}

const [jwtPayload, error] = safeParseJwt(userFromCookie);

if (error) {
console.error("Error parsing JWT", error);
return;
return { shouldClearCartCookie: false };
}

const emailFromCookie = jwtPayload?.sub;
const userIdFromCookie = jwtPayload?.userId;

if (
typeof emailFromCookie === "string" &&
typeof email === "string" &&
emailFromCookie &&
email &&
emailFromCookie !== email
) {
const headersDenyList = new Set(["cookie", "cache-control"]);
Expand All @@ -66,7 +66,11 @@ const logMismatchedCart = (cart: OrderForm, req: Request, ctx: AppContext) => {
),
),
});

return { shouldClearCartCookie: true };
}

return { shouldClearCartCookie: false };
};

export const cache = "no-store";
Expand Down Expand Up @@ -103,9 +107,11 @@ const loader = async (
const cart = await response.json() as OrderForm;

// Temporary logging to check for cart mismatch
logMismatchedCart(cart, req, ctx);
const { shouldClearCartCookie } = logMismatchedCart(cart, req, ctx);

proxySetCookie(response.headers, ctx.response.headers, req.url);
proxySetCookie(response.headers, ctx.response.headers, req.url, {
shouldClearCartCookie,
});

if (!segment?.payload) {
return forceHttpsOnAssets(cart);
Expand Down
6 changes: 6 additions & 0 deletions vtex/loaders/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ const buildProxyRoutes = (
includeScriptsToBody,
removeDirtyCookies: true,
pathsThatRequireSameReferer: VTEX_PATHS_THAT_REQUIRES_SAME_REFERER,
customHeaders: pathTemplate.includes("/checkout") ? [
{
key: "Vary",
value: "Cookie, Accept-Encoding",
},
] : [],
};

return ({
Expand Down
21 changes: 19 additions & 2 deletions vtex/utils/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export const proxySetCookie = (
from: Headers,
to: Headers,
toDomain?: URL | string,
options?: {
shouldClearCartCookie?: boolean;
},
) => {
const newDomain = toDomain && new URL(toDomain);

Expand All @@ -19,8 +22,22 @@ export const proxySetCookie = (
domain: newDomain.hostname,
}
: cookie;

setCookie(to, newCookie);

if(cookie.name === "checkout.vtex.com" && options?.shouldClearCartCookie) {

setCookie(to, {
name: "checkout.vtex.com",
value: "",
expires: new Date(0),
maxAge: 0,
path: "/",
secure: true,
httpOnly: true,
domain: newDomain ? newDomain?.hostname : "",
});
} else {
setCookie(to, newCookie);
}
}
};

Expand Down
Loading