From 5179e5c5be5fdce020940de50ace405d6f2c41ec Mon Sep 17 00:00:00 2001 From: Nils-Johan Gynther Date: Fri, 17 Apr 2026 20:19:42 +0200 Subject: [PATCH] feat(api): enhance fetchJson to include authentication headers for server requests --- frontend/lib/api.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/frontend/lib/api.ts b/frontend/lib/api.ts index db7fa518..2d2e286b 100644 --- a/frontend/lib/api.ts +++ b/frontend/lib/api.ts @@ -1,17 +1,23 @@ +import { getAuthHeaders } from './auth-headers'; + const API_BASE = process.env.NEXT_PUBLIC_API_URL_INTERNAL || 'http://recipe-api:8080'; export async function fetchJson(path: string, init?: RequestInit): Promise { // Använd alltid relativ path i webbläsaren för att undvika mixed content - const url = typeof window === 'undefined' + const isServer = typeof window === 'undefined'; + const url = isServer ? (path.startsWith('http') ? path : `${API_BASE}${path}`) : path; + const authHeaders = isServer ? await getAuthHeaders() : {}; + const res = await fetch(url, { ...init, cache: 'no-store', headers: { 'Content-Type': 'application/json', + ...authHeaders, ...(init?.headers || {}), }, });