Docs: shop runs in PocketBase hooks; secrets per service
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KgF3Kf47LjPGHKQHszpUAv
This commit is contained in:
parent
773fecc090
commit
9fab662bee
2 changed files with 14 additions and 9 deletions
13
AGENTS.md
13
AGENTS.md
|
|
@ -54,7 +54,7 @@ app-public/ # SvelteKit public app (serves *.<domain>)
|
||||||
│ │ ├── scrollReveal.ts # Re-exports from $shared
|
│ │ ├── scrollReveal.ts # Re-exports from $shared
|
||||||
│ │ ├── stripe-client.ts # Re-exports from $shared
|
│ │ ├── stripe-client.ts # Re-exports from $shared
|
||||||
│ │ ├── basket.svelte.ts # Re-exports from $shared
|
│ │ ├── basket.svelte.ts # Re-exports from $shared
|
||||||
│ │ └── server/ # Re-exports from $shared/server (pb-admin, basket); stripe.server.ts is public-only
|
│ │ └── server/ # shop.ts (client for PocketBase's /api/shop routes, basket cookie) + $shared/server re-exports
|
||||||
│ ├── routes/
|
│ ├── routes/
|
||||||
│ │ ├── +layout.svelte # Root layout (always renders Header/Footer)
|
│ │ ├── +layout.svelte # Root layout (always renders Header/Footer)
|
||||||
│ │ ├── +page.svelte # Home page (slug=home)
|
│ │ ├── +page.svelte # Home page (slug=home)
|
||||||
|
|
@ -69,6 +69,7 @@ app-public/ # SvelteKit public app (serves *.<domain>)
|
||||||
├── vite.config.ts # Tailwind v4 + SvelteKit plugins
|
├── vite.config.ts # Tailwind v4 + SvelteKit plugins
|
||||||
└── package.json
|
└── package.json
|
||||||
pocketbase/pb_migrations/ # JS migrations, mounted at /pb_migrations; applied on PocketBase start
|
pocketbase/pb_migrations/ # JS migrations, mounted at /pb_migrations; applied on PocketBase start
|
||||||
|
pocketbase/pb_hooks/ # JS hooks, mounted at /pb_hooks: the shop API (/api/shop/*) + hold-sweeping cron
|
||||||
docker-compose.yml # app-admin + app-public + pocketbase + caddy services
|
docker-compose.yml # app-admin + app-public + pocketbase + caddy services
|
||||||
Caddyfile # Subdomain-based routing
|
Caddyfile # Subdomain-based routing
|
||||||
```
|
```
|
||||||
|
|
@ -127,7 +128,8 @@ To add an email block type: add to `EmailBlockType` in `packages/shared/src/type
|
||||||
- Server load functions use `locals.pb.collection('pages').getFirstListItem()`
|
- Server load functions use `locals.pb.collection('pages').getFirstListItem()`
|
||||||
- **Always build filters with `pb.filter('slug = {:slug}', { slug })`** — never interpolate values into filter strings (quotes break them).
|
- **Always build filters with `pb.filter('slug = {:slug}', { slug })`** — never interpolate values into filter strings (quotes break them).
|
||||||
- **Schema changes**: add a migration to `pocketbase/pb_migrations/` (runs once, on the next PocketBase start) **and** mirror it in `pb_schema.json` (fresh installs import that after the migrations have already run). Migrations must skip missing collections and must not throw for data problems — a failing migration stops PocketBase from starting.
|
- **Schema changes**: add a migration to `pocketbase/pb_migrations/` (runs once, on the next PocketBase start) **and** mirror it in `pb_schema.json` (fresh installs import that after the migrations have already run). Migrations must skip missing collections and must not throw for data problems — a failing migration stops PocketBase from starting.
|
||||||
- **API rules**: CMS collections require `@request.auth.id != ''`, so `users` must never allow public sign-up (its createRule is superuser-only). Commerce writes (orders, baskets, basket_lines) are superuser-only; the public app does them server-side via `getAdminPb()`.
|
- **API rules**: CMS collections require `@request.auth.id != ''`, so `users` must never allow public sign-up (its createRule is superuser-only). Commerce writes (orders, order_lines, baskets, basket_lines) are superuser-only; they happen in the PocketBase shop hooks (`pocketbase/pb_hooks/shop/lib.js`), which bypass rules.
|
||||||
|
- **Shop / payments**: all basket, slot-hold, Stripe and order logic lives in the PocketBase hooks (JSVM: synchronous, each handler `require()`s the lib; see the header comment). The public app must stay unprivileged — no superuser login, no Stripe secret key; its routes only proxy to `/api/shop/*` and manage the `basket_id` cookie (`app-public/src/lib/server/shop.ts`). Booking emails are rendered and sent by the public app from data the webhook hook returns (`OrderEmailData`, `sendOrderEmailsFromData`); keep `orderEmailData()` in the hook and `loadOrderEmailData()` in `packages/shared/src/server/email` returning the same shape. Local testing uses `stripe-mock` (`STRIPE_API_BASE`, local compose only).
|
||||||
|
|
||||||
### Styling
|
### Styling
|
||||||
- **Public site**: Custom theme via `@theme` in `app.css` — colors: `warm`, `charcoal`, `sage`, `sage-dark`, `terracotta`. Fonts: `DM Sans` (body), `Playfair Display` (headings).
|
- **Public site**: Custom theme via `@theme` in `app.css` — colors: `warm`, `charcoal`, `sage`, `sage-dark`, `terracotta`. Fonts: `DM Sans` (body), `Playfair Display` (headings).
|
||||||
|
|
@ -159,7 +161,7 @@ DOMAIN=localhost docker compose up --build
|
||||||
|
|
||||||
# Type checking (needs node_modules in packages/shared too, or every shared
|
# Type checking (needs node_modules in packages/shared too, or every shared
|
||||||
# file that imports a package fails with "Cannot find module")
|
# file that imports a package fails with "Cannot find module")
|
||||||
cd packages/shared && npm ci
|
cd packages/shared && npm install # no lockfile here, so not `npm ci`
|
||||||
cd app-admin && npm run check
|
cd app-admin && npm run check
|
||||||
cd app-public && npm run check
|
cd app-public && npm run check
|
||||||
|
|
||||||
|
|
@ -177,5 +179,6 @@ cd app-public && npm run build
|
||||||
- `PUBLIC_SITE_URL` — public site URL used by the admin app for cross-subdomain preview links (`http://www.${DOMAIN}:8080`).
|
- `PUBLIC_SITE_URL` — public site URL used by the admin app for cross-subdomain preview links (`http://www.${DOMAIN}:8080`).
|
||||||
- `SMTP_HOST`, `SMTP_PORT`, `SMTP_USER`, `SMTP_PASS`, `EMAIL_FROM`, `EMAIL_FROM_NAME` — Brevo SMTP relay for booking emails (both apps). Empty `SMTP_HOST` = log emails instead of sending.
|
- `SMTP_HOST`, `SMTP_PORT`, `SMTP_USER`, `SMTP_PASS`, `EMAIL_FROM`, `EMAIL_FROM_NAME` — Brevo SMTP relay for booking emails (both apps). Empty `SMTP_HOST` = log emails instead of sending.
|
||||||
- `SITE_TIMEZONE` — IANA timezone for booking times in emails (default `Europe/Oslo`).
|
- `SITE_TIMEZONE` — IANA timezone for booking times in emails (default `Europe/Oslo`).
|
||||||
- Stripe + PocketBase admin secrets are read from a root-level `.env` next to `docker-compose.yml`; see `.env.example`.
|
- Secrets (Stripe, `INTERNAL_API_SECRET`, SMTP) are read from a root-level `.env` next to `docker-compose.yml`; see `.env.example`.
|
||||||
- `POCKETBASE_ADMIN_EMAIL` / `POCKETBASE_ADMIN_PASSWORD` are required for **any** commerce data: without them the public app logs `Commerce data unavailable` and Shop blocks show "Nothing is available". Shop data is loaded by `loadShopData()` in `packages/shared/src/server/shop.ts` (public layout and admin `/preview`).
|
- `STRIPE_SECRET_KEY`, `STRIPE_WEBHOOK_SECRET` → PocketBase only. `INTERNAL_API_SECRET` → PocketBase and app-public (booking-email status). No app gets PocketBase superuser credentials.
|
||||||
|
- Shop listings are public data, loaded anonymously by `loadShopData()` in `packages/shared/src/server/shop.ts` (public layout and admin `/preview`); if a Shop block has nothing to show it says "Nothing is available".
|
||||||
|
|
|
||||||
10
README.md
10
README.md
|
|
@ -79,7 +79,7 @@ If the `globals` records do not exist, the app falls back to safe defaults and t
|
||||||
|
|
||||||
### Migrations
|
### Migrations
|
||||||
|
|
||||||
`pocketbase/pb_migrations/` is mounted into the PocketBase container, which applies any new migration once when it starts (`docker compose up` / restart). The first one (`1791244800_lock_rules_dates_unique.js`) closes public sign-up on the `users` collection, makes order/basket writes superuser-only, adds `created`/`updated` dates and unique slug/key indexes. On an existing install, check the PocketBase log after restarting — if duplicate page slugs or global keys exist, the unique index is skipped with a log line.
|
`pocketbase/pb_migrations/` is mounted into the PocketBase container, which applies any new migration once when it starts (`docker compose up` / restart). The first one (`1791244800_lock_rules_dates_unique.js`) closes public sign-up on the `users` collection, makes order/basket writes superuser-only, adds `created`/`updated` dates and unique slug/key indexes. On an existing install, check the PocketBase log after restarting — if duplicate page slugs or global keys exist, the unique index is skipped with a log line. `1791331200_lock_order_lines.js` makes `order_lines` writes superuser-only (they were public).
|
||||||
|
|
||||||
> **Security:** every CMS collection allows writes to *any* signed-in user, so the `users` collection must not allow public sign-up. The migration enforces this; if you manage PocketBase by hand, keep `users` → API rules → Create set to superuser-only.
|
> **Security:** every CMS collection allows writes to *any* signed-in user, so the `users` collection must not allow public sign-up. The migration enforces this; if you manage PocketBase by hand, keep `users` → API rules → Create set to superuser-only.
|
||||||
|
|
||||||
|
|
@ -136,10 +136,12 @@ All routes in `app-admin` except `/login` are guarded in `src/hooks.server.ts`.
|
||||||
|
|
||||||
The shop/checkout blocks implement a basket-based booking flow: visitors add services (with time slots) or products to a server-side basket, then pay with an embedded Stripe Payment Element. Order creation is confirmed by the Stripe **webhook** (never the client).
|
The shop/checkout blocks implement a basket-based booking flow: visitors add services (with time slots) or products to a server-side basket, then pay with an embedded Stripe Payment Element. Order creation is confirmed by the Stripe **webhook** (never the client).
|
||||||
|
|
||||||
|
**Where the logic lives:** all commerce writes (baskets, slot holds, Stripe PaymentIntents, the webhook that creates orders) run *inside PocketBase* as JS hooks — `pocketbase/pb_hooks/shop/lib.js`, routes under `/api/shop/*`, plus a once-a-minute cron that releases expired holds. The public app holds **no PocketBase or Stripe secret**: its `/api/basket`, `/api/checkout/create-intent`, `/api/webhooks/stripe` and `/checkout/return` only forward to those routes and keep the basket cookie. A compromised public app therefore can't read or change the database beyond what any visitor can. Its one privileged call records booking-email status on an order, authorised by `INTERNAL_API_SECRET`.
|
||||||
|
|
||||||
Setup:
|
Setup:
|
||||||
|
|
||||||
1. Add the Stripe + PocketBase-admin vars from `.env.example` to `.env` at the project root.
|
1. Add the Stripe vars and `INTERNAL_API_SECRET` (e.g. `openssl rand -hex 32`) from `.env.example` to `.env` at the project root. docker-compose gives the Stripe secret key and webhook secret to **PocketBase only**.
|
||||||
2. `POCKETBASE_ADMIN_EMAIL` / `POCKETBASE_ADMIN_PASSWORD` are the credentials of the PocketBase superuser you created — the basket/checkout/webhook APIs use a server-side admin client so collection rules can stay admin-only.
|
2. Hook changes apply on PocketBase restart (the directory is mounted read-only at `/pb_hooks`).
|
||||||
3. For local webhook testing, forward Stripe events with the CLI:
|
3. For local webhook testing, forward Stripe events with the CLI:
|
||||||
```sh
|
```sh
|
||||||
stripe listen --forward-to www.localhost:8080/api/webhooks/stripe
|
stripe listen --forward-to www.localhost:8080/api/webhooks/stripe
|
||||||
|
|
@ -150,7 +152,7 @@ Setup:
|
||||||
|
|
||||||
## Booking emails (Brevo)
|
## Booking emails (Brevo)
|
||||||
|
|
||||||
After the Stripe webhook creates an order it sends two emails through an SMTP relay (Brevo) with `nodemailer`:
|
After the Stripe webhook creates an order (in PocketBase, which hands the order data back to the public app's webhook route) the public app sends two emails through an SMTP relay (Brevo) with `nodemailer`:
|
||||||
|
|
||||||
- **Booking confirmation** to the customer, with a `booking.ics` calendar attachment for booked time slots. Reply-To is the footer email.
|
- **Booking confirmation** to the customer, with a `booking.ics` calendar attachment for booked time slots. Reply-To is the footer email.
|
||||||
- **New booking alert** to the studio (the template's recipients, or the footer email). Reply-To is the customer.
|
- **New booking alert** to the studio (the template's recipients, or the footer email). Reply-To is the customer.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue