Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phpsessid #636

Merged
merged 2 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion magento/loaders/user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Person } from "../../commerce/types.ts";
import { AppContext } from "../mod.ts";
import { getUserCookie, SESSION_COOKIE } from "../utils/user.ts";
import { SESSION_COOKIE } from "../utils/constants.ts";
import { getUserCookie } from "../utils/user.ts";

async function loader(
_props: unknown,
Expand Down
15 changes: 14 additions & 1 deletion magento/utils/cart.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { getCookies } from "std/http/cookie.ts";
import { getCookies, setCookie } from "std/http/cookie.ts";
import { AppContext } from "../mod.ts";
import { Cart, MagentoCardPrices, MagentoProduct } from "./client/types.ts";
import { toURL } from "./transform.ts";
import { SESSION_COOKIE } from "./constants.ts";
import { generateUniqueIdentifier } from "./hash.ts";

const CART_COOKIE = "dataservices_cart_id";
const CART_CUSTOMER_COOKIE = "dataservices_customer_id";
Expand Down Expand Up @@ -29,6 +31,17 @@ export async function createCart(

const customerCookie = getCookies(headers)[CART_CUSTOMER_COOKIE];

const sessionCookie = getCookies(headers)[SESSION_COOKIE];

if (!sessionCookie) {
setCookie(headers, {
path: "/",
maxAge: ONE_WEEK_MS,
name: SESSION_COOKIE,
value: (await generateUniqueIdentifier()).hash,
});
}

if (!cartCookie && !customerCookie) {
const tokenCart = await clientAdmin["POST /rest/:site/V1/guest-carts"]({
site,
Expand Down
1 change: 1 addition & 0 deletions magento/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,4 @@ export const BASE_CURRENCY_CODE = "base_currency_code";
//Fields products sku
export const SKU = "sku";
export const MEDIA_GALLERY_ENTRIES = "media_gallery_entries";
export const SESSION_COOKIE = "PHPSESSID";
18 changes: 18 additions & 0 deletions magento/utils/hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export async function sha256Hash(input: string) {
const utf8Encoder = new TextEncoder();
const inputBytes = utf8Encoder.encode(input);

const hashBuffer = await crypto.subtle.digest("SHA-256", inputBytes);

return Array.prototype.map.call(new Uint8Array(hashBuffer), (byte) => {
return ("00" + byte.toString(16)).slice(-2);
}).join("");
}

export async function generateUniqueIdentifier() {
const timestamp = new Date().getTime();
const randomComponent = Math.floor(Math.random() * 1000000);
const inputForHash = `${timestamp}${randomComponent}`;
const sha256 = await sha256Hash(inputForHash);
return { hash: sha256, timestamp };
}
3 changes: 1 addition & 2 deletions magento/utils/user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { getCookies } from "std/http/cookie.ts";

export const SESSION_COOKIE = "PHPSESSID";
import { SESSION_COOKIE } from "../utils/constants.ts";

export const getUserCookie = (headers: Headers): string | undefined => {
const cookies = getCookies(headers);
Expand Down
Loading