fix warnings on cart page refresh

This commit is contained in:
2025-09-10 06:32:48 +03:00
parent cb11244d79
commit 229b3d7c27
2 changed files with 37 additions and 26 deletions

View File

@@ -1,12 +1,20 @@
import "server-only"
import { cookies as nextCookies } from "next/headers"
const CookieName = {
MEDUSA_CUSTOMER_ID: "_medusa_customer_id",
MEDUSA_JWT: "_medusa_jwt",
MEDUSA_CART_ID: "_medusa_cart_id",
MEDUSA_CACHE_ID: "_medusa_cache_id",
}
export const getAuthHeaders = async (): Promise<
{ authorization: string } | {}
> => {
try {
const cookies = await nextCookies()
const token = cookies.get("_medusa_jwt")?.value
const token = cookies.get(CookieName.MEDUSA_JWT)?.value
if (!token) {
return {}
@@ -23,7 +31,7 @@ export const getMedusaCustomerId = async (): Promise<
> => {
try {
const cookies = await nextCookies()
const customerId = cookies.get("_medusa_customer_id")?.value
const customerId = cookies.get(CookieName.MEDUSA_CUSTOMER_ID)?.value
if (!customerId) {
return { customerId: null }
@@ -31,14 +39,14 @@ export const getMedusaCustomerId = async (): Promise<
return { customerId }
} catch {
return { customerId: null}
return { customerId: null }
}
}
export const getCacheTag = async (tag: string): Promise<string> => {
try {
const cookies = await nextCookies()
const cacheId = cookies.get("_medusa_cache_id")?.value
const cacheId = cookies.get(CookieName.MEDUSA_CACHE_ID)?.value
if (!cacheId) {
return ""
@@ -66,51 +74,51 @@ export const getCacheOptions = async (
return { tags: [`${cacheTag}`] }
}
const getCookieSharedOptions = () => ({
maxAge: 60 * 60 * 24 * 7,
httpOnly: false,
secure: process.env.NODE_ENV === "production",
});
const getCookieResetOptions = () => ({
maxAge: -1,
});
export const setAuthToken = async (token: string) => {
const cookies = await nextCookies()
cookies.set("_medusa_jwt", token, {
maxAge: 60 * 60 * 24 * 7,
httpOnly: true,
sameSite: "strict",
secure: process.env.NODE_ENV === "production",
cookies.set(CookieName.MEDUSA_JWT, token, {
...getCookieSharedOptions(),
})
}
export const setMedusaCustomerId = async (customerId: string) => {
const cookies = await nextCookies()
cookies.set("_medusa_customer_id", customerId, {
maxAge: 60 * 60 * 24 * 7,
httpOnly: true,
sameSite: "strict",
secure: process.env.NODE_ENV === "production",
cookies.set(CookieName.MEDUSA_CUSTOMER_ID, customerId, {
...getCookieSharedOptions(),
})
}
export const removeAuthToken = async () => {
const cookies = await nextCookies()
cookies.set("_medusa_jwt", "", {
maxAge: -1,
cookies.set(CookieName.MEDUSA_JWT, "", {
...getCookieResetOptions(),
})
}
export const getCartId = async () => {
const cookies = await nextCookies()
return cookies.get("_medusa_cart_id")?.value
return cookies.get(CookieName.MEDUSA_CART_ID)?.value
}
export const setCartId = async (cartId: string) => {
const cookies = await nextCookies()
cookies.set("_medusa_cart_id", cartId, {
maxAge: 60 * 60 * 24 * 7,
httpOnly: true,
sameSite: "strict",
secure: process.env.NODE_ENV === "production",
cookies.set(CookieName.MEDUSA_CART_ID, cartId, {
...getCookieSharedOptions(),
})
}
export const removeCartId = async () => {
const cookies = await nextCookies()
cookies.set("_medusa_cart_id", "", {
maxAge: -1,
cookies.set(CookieName.MEDUSA_CART_ID, "", {
...getCookieResetOptions(),
})
}