From 364e80826136fd113b85d5179f5c82b26ae3100c Mon Sep 17 00:00:00 2001 From: MacRimi Date: Thu, 13 Nov 2025 17:14:47 +0100 Subject: [PATCH] Update api-config.ts --- AppImage/lib/api-config.ts | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/AppImage/lib/api-config.ts b/AppImage/lib/api-config.ts index 8ee2ff6..63e8a66 100644 --- a/AppImage/lib/api-config.ts +++ b/AppImage/lib/api-config.ts @@ -60,6 +60,18 @@ export function getApiUrl(endpoint: string): string { return `${baseUrl}${normalizedEndpoint}` } +/** + * Gets the JWT token from localStorage + * + * @returns JWT token or null if not authenticated + */ +export function getAuthToken(): string | null { + if (typeof window === "undefined") { + return null + } + return localStorage.getItem("proxmenux-auth-token") +} + /** * Fetches data from an API endpoint with error handling * @@ -70,12 +82,20 @@ export function getApiUrl(endpoint: string): string { export async function fetchApi(endpoint: string, options?: RequestInit): Promise { const url = getApiUrl(endpoint) + const token = getAuthToken() + + const headers: Record = { + "Content-Type": "application/json", + ...(options?.headers as Record), + } + + if (token) { + headers["Authorization"] = `Bearer ${token}` + } + const response = await fetch(url, { ...options, - headers: { - "Content-Type": "application/json", - ...options?.headers, - }, + headers, cache: "no-store", })