feat: integrate authFetch for API calls in InventoryView and PantryView, and add pantry route with authentication

This commit is contained in:
Nils-Johan Gynther
2026-04-21 14:49:20 +02:00
parent 81b63b3fdb
commit c57f4bde19
3 changed files with 22 additions and 5 deletions
+13
View File
@@ -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' } });
});
@@ -4,19 +4,21 @@ import { useState, useEffect, useCallback } from 'react';
import type { InventoryItem, Product } from '../../../../features/inventory/types'; import type { InventoryItem, Product } from '../../../../features/inventory/types';
import InventoryList from '../../../inventory/InventoryList'; import InventoryList from '../../../inventory/InventoryList';
import InventoryForm from '../../../inventory/InventoryForm'; import InventoryForm from '../../../inventory/InventoryForm';
import { useAuthFetch } from '../../../../lib/use-auth-fetch';
export default function InventoryView() { export default function InventoryView() {
const [inventory, setInventory] = useState<InventoryItem[]>([]); const [inventory, setInventory] = useState<InventoryItem[]>([]);
const [products, setProducts] = useState<Product[]>([]); const [products, setProducts] = useState<Product[]>([]);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null); const [error, setError] = useState<string | null>(null);
const authFetch = useAuthFetch();
const load = useCallback(async () => { const load = useCallback(async () => {
setLoading(true); setLoading(true);
setError(null); setError(null);
try { try {
const [invRes, prodRes] = await Promise.all([ const [invRes, prodRes] = await Promise.all([
fetch('/api/inventory'), authFetch('/api/inventory'),
fetch('/api/products'), fetch('/api/products'),
]); ]);
if (!invRes.ok) throw new Error('Kunde inte hämta inventarie'); if (!invRes.ok) throw new Error('Kunde inte hämta inventarie');
@@ -29,7 +31,7 @@ export default function InventoryView() {
} finally { } finally {
setLoading(false); setLoading(false);
} }
}, []); }, [authFetch]);
useEffect(() => { load(); }, [load]); useEffect(() => { load(); }, [load]);
@@ -4,6 +4,7 @@ import { useState, useEffect, useCallback } from 'react';
import type { Product } from '../../../../features/inventory/types'; import type { Product } from '../../../../features/inventory/types';
import AddToPantryForm from '../../../baslager/AddToPantryForm'; import AddToPantryForm from '../../../baslager/AddToPantryForm';
import PantryList from '../../../baslager/PantryList'; import PantryList from '../../../baslager/PantryList';
import { useAuthFetch } from '../../../../lib/use-auth-fetch';
type PantryItem = { type PantryItem = {
id: number; id: number;
@@ -25,15 +26,16 @@ export default function PantryView() {
const [inventoryByProductId, setInventoryByProductId] = useState<Record<number, InventoryItem[]>>({}); const [inventoryByProductId, setInventoryByProductId] = useState<Record<number, InventoryItem[]>>({});
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null); const [error, setError] = useState<string | null>(null);
const authFetch = useAuthFetch();
const load = useCallback(async () => { const load = useCallback(async () => {
setLoading(true); setLoading(true);
setError(null); setError(null);
try { try {
const [pantryRes, prodRes, invRes] = await Promise.all([ const [pantryRes, prodRes, invRes] = await Promise.all([
fetch('/api/pantry'), authFetch('/api/pantry'),
fetch('/api/products'), 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 (!pantryRes.ok) throw new Error('Kunde inte hämta baslager');
if (!prodRes.ok) throw new Error('Kunde inte hämta produkter'); if (!prodRes.ok) throw new Error('Kunde inte hämta produkter');
@@ -52,7 +54,7 @@ export default function PantryView() {
} finally { } finally {
setLoading(false); setLoading(false);
} }
}, []); }, [authFetch]);
useEffect(() => { load(); }, [load]); useEffect(() => { load(); }, [load]);