diff --git a/frontend/app/api/pantry/route.ts b/frontend/app/api/pantry/route.ts new file mode 100644 index 00000000..7a6d685d --- /dev/null +++ b/frontend/app/api/pantry/route.ts @@ -0,0 +1,13 @@ +import { NextResponse } from 'next/server'; +import { withAuth } from '../../../lib/with-auth'; + +const API_BASE = process.env.NEXT_PUBLIC_API_URL_INTERNAL || 'http://recipe-api:8080'; + +export const GET = withAuth(async (_request, session) => { + const res = await fetch(`${API_BASE}/api/pantry`, { + headers: { Authorization: `Bearer ${session.accessToken}` }, + cache: 'no-store', + }); + const text = await res.text(); + return new NextResponse(text, { status: res.status, headers: { 'Content-Type': 'application/json' } }); +}); diff --git a/frontend/app/profil/tabs/views/InventoryView.tsx b/frontend/app/profil/tabs/views/InventoryView.tsx index 0d2ddb30..9cf9f4ec 100644 --- a/frontend/app/profil/tabs/views/InventoryView.tsx +++ b/frontend/app/profil/tabs/views/InventoryView.tsx @@ -4,19 +4,21 @@ import { useState, useEffect, useCallback } from 'react'; import type { InventoryItem, Product } from '../../../../features/inventory/types'; import InventoryList from '../../../inventory/InventoryList'; import InventoryForm from '../../../inventory/InventoryForm'; +import { useAuthFetch } from '../../../../lib/use-auth-fetch'; export default function InventoryView() { const [inventory, setInventory] = useState([]); const [products, setProducts] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); + const authFetch = useAuthFetch(); const load = useCallback(async () => { setLoading(true); setError(null); try { const [invRes, prodRes] = await Promise.all([ - fetch('/api/inventory'), + authFetch('/api/inventory'), fetch('/api/products'), ]); if (!invRes.ok) throw new Error('Kunde inte hämta inventarie'); @@ -29,7 +31,7 @@ export default function InventoryView() { } finally { setLoading(false); } - }, []); + }, [authFetch]); useEffect(() => { load(); }, [load]); diff --git a/frontend/app/profil/tabs/views/PantryView.tsx b/frontend/app/profil/tabs/views/PantryView.tsx index 3fc38deb..8f9ae437 100644 --- a/frontend/app/profil/tabs/views/PantryView.tsx +++ b/frontend/app/profil/tabs/views/PantryView.tsx @@ -4,6 +4,7 @@ import { useState, useEffect, useCallback } from 'react'; import type { Product } from '../../../../features/inventory/types'; import AddToPantryForm from '../../../baslager/AddToPantryForm'; import PantryList from '../../../baslager/PantryList'; +import { useAuthFetch } from '../../../../lib/use-auth-fetch'; type PantryItem = { id: number; @@ -25,15 +26,16 @@ export default function PantryView() { const [inventoryByProductId, setInventoryByProductId] = useState>({}); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); + const authFetch = useAuthFetch(); const load = useCallback(async () => { setLoading(true); setError(null); try { const [pantryRes, prodRes, invRes] = await Promise.all([ - fetch('/api/pantry'), + authFetch('/api/pantry'), fetch('/api/products'), - fetch('/api/inventory').catch(() => null), + authFetch('/api/inventory').catch(() => null), ]); if (!pantryRes.ok) throw new Error('Kunde inte hämta baslager'); if (!prodRes.ok) throw new Error('Kunde inte hämta produkter'); @@ -52,7 +54,7 @@ export default function PantryView() { } finally { setLoading(false); } - }, []); + }, [authFetch]); useEffect(() => { load(); }, [load]);