diff --git a/src/routes/products/[productId]/+page.server.ts b/src/routes/products/[productId]/+page.server.ts new file mode 100644 index 0000000..d99e062 --- /dev/null +++ b/src/routes/products/[productId]/+page.server.ts @@ -0,0 +1,35 @@ +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 + } + + // 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 6e81008..e000679 100644 --- a/src/routes/products/[productId]/+page.svelte +++ b/src/routes/products/[productId]/+page.svelte @@ -1,11 +1,15 @@
@@ -370,27 +351,50 @@ - + + + + Add to basket + + {:else} + + {/if}