feat(api): add create and update product routes with authentication

refactor(admin): integrate router refresh after product updates in forms
fix(imports): update fetch paths for product creation and update in ReceiptImportClient
This commit is contained in:
Nils-Johan Gynther
2026-04-19 18:12:29 +02:00
parent 90b02c4690
commit f12d881395
7 changed files with 177 additions and 8 deletions
+74
View File
@@ -49,6 +49,80 @@ docker exec recipe-db mariadb -uroot -p"LÖSENORD" recipe_app -e "SHOW TABLES;"
---
## Caddy-konfiguration (reverse proxy)
Caddy sitter framför applikationen och distribuerar trafik. Ordningen på `handle`-blocken är kritisk — Caddy väljer det första matchande blocket.
```caddy
recept.gynther.se {
import common
# === IMPORT SERVICE (Document Converter) ===
# Måste komma FÖRE backend-reglerna
handle /api/recipes/import* {
reverse_proxy recipe-import-service:3000
}
# === NEXT.JS PROXY-ROUTES (frontend-hanterade) ===
# Dessa routes innehåller server-side logic (auth, aggregering, mm)
# och måste gå till frontend, INTE backend
handle /api/inventory-history-proxy {
reverse_proxy recipe-frontend:3000
}
handle /api/admin/merge-preview-proxy {
reverse_proxy recipe-frontend:3000
}
handle /api/recipe-preview-proxy {
reverse_proxy recipe-frontend:3000
}
# === BACKEND API-ENDPOINTS ===
handle /api/products* {
reverse_proxy recipe-api:8080
}
handle /api/inventory* {
reverse_proxy recipe-api:8080
}
handle /api/recipes* {
reverse_proxy recipe-api:8080
}
handle /health {
reverse_proxy recipe-api:8080
}
# === CATCH-ALL: Övriga /api/* → frontend ===
# Fångar upp alla Next.js API routes som inte explicit
# listats ovan, t.ex. /api/admin/*, /api/auth/*, /api/categories, mm.
handle /api/* {
reverse_proxy recipe-frontend:3000
}
# Alla övriga requests → frontend
reverse_proxy /* recipe-frontend:3000
}
```
### Viktig regel: Caddy-routing och Next.js API routes
> **Caddy-regeln `handle /api/products*` fångar upp ALLT som börjar med `/api/products`** — inklusive sökvägar som `/api/products-create` eller `/api/products-update`. Det innebär att Next.js API routes som ska hanteras av frontend-containern **INTE** får ha sökvägar som börjar med `products`, `inventory`, `recipes` eller andra prefix som Caddy skickar till backend.
Next.js API routes som kräver server-side auth och ska gå via frontend måste ha prefix som hamnar i catch-all-blocket `handle /api/* → recipe-frontend:3000`. Exempel på säkra prefix:
| Prefix | Caddy-regel | Destination |
|--------|-------------|-------------|
| `/api/admin/*` | catch-all | `recipe-frontend:3000` |
| `/api/categories` | catch-all | `recipe-frontend:3000` |
| `/api/auth/*` | catch-all | `recipe-frontend:3000` |
| `/api/products*` | explicit regel | `recipe-api:8080` ⚠️ |
| `/api/inventory*` | explicit regel | `recipe-api:8080` ⚠️ |
## Frontend
- **Framework:** Next.js 16.2 (App Router, server + client components)