feat: update Caddy routing documentation to include pantry API endpoint and emphasize useAuthFetch for client components

This commit is contained in:
Nils-Johan Gynther
2026-04-21 14:52:20 +02:00
parent c57f4bde19
commit 69f05e6b43
+4 -1
View File
@@ -413,7 +413,7 @@ Alla proxy-routes använder `withAuth(handler)`-wrappern (från `lib/with-auth.t
### Kritisk regel: Caddy-routing och klientkomponenter
Caddy routar `/api/recipes*`, `/api/products*` och `/api/inventory*` **direkt till NestJS** — Next.js route handlers körs aldrig för dessa sökvägar. Det innebär att klientkomponenter (`'use client'`) som gör `fetch()` till dessa endpoints **inte kan lita på att withAuth eller getAuthHeaders lägger till token automatiskt**.
Caddy routar `/api/recipes*`, `/api/products*`, `/api/inventory*` och `/api/pantry*` **direkt till NestJS** — Next.js route handlers körs aldrig för dessa sökvägar. Det innebär att klientkomponenter (`'use client'`) som gör `fetch()` till dessa endpoints **inte kan lita på att withAuth eller getAuthHeaders lägger till token automatiskt**.
**Lösning: använd alltid `useAuthFetch()` i klientkomponenter:**
@@ -438,10 +438,13 @@ Hooken hämtar token via `useSession()` (kräver att `SessionProvider` finns i l
| `/api/recipes*` | `recipe-api:8080` direkt | ✅ Ja |
| `/api/products*` | `recipe-api:8080` direkt | ✅ Ja |
| `/api/inventory*` | `recipe-api:8080` direkt | ✅ Ja |
| `/api/pantry*` | `recipe-api:8080` direkt | ✅ Ja |
| `/api/admin/*` | `recipe-frontend:3000` via catch-all | Nej (withAuth hanterar) |
| `/api/auth/*` | `recipe-frontend:3000` via catch-all | Nej |
| `/api/categories` | `recipe-frontend:3000` via catch-all | Nej |
> ⚠️ **Återkommande misstag (2026-04-21):** Vid implementationen av `InventoryView` och `PantryView` (klientkomponenter i Databas-tabben) användes plain `fetch('/api/inventory')` och `fetch('/api/pantry')` utan token. NestJS returnerade 401 och komponenterna visade "Kunde inte hämta inventarie". Felet uppstod trots att en Next.js API-route för `/api/inventory` redan existerade — Caddy routar dessa paths förbi Next.js helt. **Regel: varje ny klientkomponent som hämtar data från `/api/inventory*`, `/api/pantry*`, `/api/recipes*` eller `/api/products*` måste använda `useAuthFetch()`.**
- Svenska felmeddelanden via `lib/error-handler.ts` (`parseErrorResponse`)
- Centraliserad API-access via `lib/api.ts` (`fetchJson`) — för server-side anrop
- Klientside auth-fetch via `lib/use-auth-fetch.ts` (`useAuthFetch`) — för klientkomponenter som pratar direkt med NestJS