The public (internet-facing) app logged in as the PocketBase superuser and held the Stripe secret key, so any bug in it could become full control of the database. Baskets, slot holds, Stripe PaymentIntents, the webhook that creates orders, and the payment-result lookup now run in PocketBase JS hooks (pocketbase/pb_hooks/shop, routes under /api/shop/*, plus a cron that releases expired holds every minute). The public app's routes keep their URLs but only proxy to those routes and manage the basket cookie; it has no PocketBase or Stripe secret. Booking emails are still rendered and sent by the public app (same renderer as the admin preview) from order data the webhook hook returns; it records the outcome via a narrow endpoint guarded by INTERNAL_API_SECRET. Also: slot add and order creation are now transactional; shop listings load anonymously (already public via API rules); shop API errors always reach the browser as JSON; the superuser client and stripe package are removed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KgF3Kf47LjPGHKQHszpUAv
39 lines
1.5 KiB
TypeScript
39 lines
1.5 KiB
TypeScript
import { defaultColors, defaultFooter, defaultHeader } from '$lib/types';
|
|
import type { ItemRecord, SlotRecord } from '$lib/types';
|
|
import { BASKET_COOKIE, getBasket } from '$lib/server/shop';
|
|
import { loadShopData } from '$shared/server/shop';
|
|
import type { LayoutServerLoad } from './$types';
|
|
|
|
async function getGlobal(pb: App.Locals['pb'], key: string, fallback: unknown) {
|
|
try {
|
|
const record = await pb.collection('globals').getFirstListItem(pb.filter('key = {:key}', { key }));
|
|
return record.data || fallback;
|
|
} catch {
|
|
return fallback;
|
|
}
|
|
}
|
|
|
|
export const load: LayoutServerLoad = async ({ locals, cookies }) => {
|
|
const [header, footer, colors] = await Promise.all([
|
|
getGlobal(locals.pb, 'header', defaultHeader),
|
|
getGlobal(locals.pb, 'footer', defaultFooter),
|
|
getGlobal(locals.pb, 'colors', defaultColors)
|
|
]);
|
|
|
|
if (!header.homePage) header.homePage = defaultHeader.homePage;
|
|
|
|
// Commerce data for the public site. The listing is public data (API
|
|
// rules allow active items / available slots); the basket comes from the
|
|
// shop API in PocketBase. Failures leave the shop empty, never break a page.
|
|
const [shop, basket] = await Promise.all([
|
|
loadShopData(locals.pb).catch((e) => {
|
|
console.warn('Shop data unavailable (shop blocks will be empty):', e instanceof Error ? e.message : e);
|
|
return null;
|
|
}),
|
|
getBasket(cookies.get(BASKET_COOKIE)).catch(() => null)
|
|
]);
|
|
const items: ItemRecord[] = shop?.items ?? [];
|
|
const slots: SlotRecord[] = shop?.slots ?? [];
|
|
|
|
return { header, footer, colors, user: locals.user, basket, items, slots };
|
|
};
|