From 69f05e6b43615a412e4ecf908c545ba53e83e785 Mon Sep 17 00:00:00 2001 From: Nils-Johan Gynther Date: Tue, 21 Apr 2026 14:52:20 +0200 Subject: [PATCH] feat: update Caddy routing documentation to include pantry API endpoint and emphasize useAuthFetch for client components --- TEKNISK_BESKRIVNING.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/TEKNISK_BESKRIVNING.md b/TEKNISK_BESKRIVNING.md index dd4abc37..f110fb70 100644 --- a/TEKNISK_BESKRIVNING.md +++ b/TEKNISK_BESKRIVNING.md @@ -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