diff --git a/src/routes/products/[productId]/+page.server.ts b/src/routes/products/[productId]/+page.server.ts new file mode 100644 index 0000000..6a3fba3 --- /dev/null +++ b/src/routes/products/[productId]/+page.server.ts @@ -0,0 +1,36 @@ +import { redirect } from '@sveltejs/kit'; +import { hasProductAccess } from '$lib/utils/supabase/admin'; + +export const load = async ({ params, locals: { safeGetSession }, fetch }) => { + const { session } = await safeGetSession(); + if (!session) { + throw redirect(303, '/login'); // Redirect unauthenticated users to login + } + + const productResponse = await fetch(`/products/${params.productId}`); + const product = await productResponse.json(); + + // Create the item object + const item = { + id: product.id, + name: product.name, + categoryDescription: product.description, + imgAlt: product.description, + imgSrc: product.images[0], + price: 0, + priceId: product.default_price, + quantity: 1, + isSubscription: product.isSubscription + } + + // Fetch the price of the product + const priceResponse = await fetch(`/products/prices/${product.default_price}`); + const price = await priceResponse.json(); + // Set the price of the product + item.price = price.unit_amount / 100; + + // Check if the user has access to the 'EXAMPLE PRODUCT' product + const hasPurchasedProduct = await hasProductAccess(session.user.id, params.productId); + + return { params, hasPurchasedProduct, item }; +}; diff --git a/src/routes/products/[productId]/+page.svelte b/src/routes/products/[productId]/+page.svelte index 77f5fed..b7b1092 100644 --- a/src/routes/products/[productId]/+page.svelte +++ b/src/routes/products/[productId]/+page.svelte @@ -1,11 +1,15 @@
@@ -98,9 +78,6 @@ class="data w-full lg:pr-8 pr-0 xl:justify-start justify-center flex items-center max-lg:pb-10 xl:my-2 lg:my-5 my-0" >
-

{item.name}

@@ -290,33 +267,8 @@ all size is available - - {#if !item.isSubscription} + {#if !item.isSubscription && !hasPurchasedProduct}
-
- {:else} -
- -
+ {:else} + {#if hasPurchasedProduct} +
+ +
+ {/if} + {#if item.isSubscription} +
+ +
+ {/if} {/if}
diff --git a/src/routes/products/[productId]/+page.ts b/src/routes/products/[productId]/+page.ts deleted file mode 100644 index fd0e3fa..0000000 --- a/src/routes/products/[productId]/+page.ts +++ /dev/null @@ -1,4 +0,0 @@ -/** @type {import('./$types').PageLoad} */ -export function load({ params }) { - return params; -}