Update api-config.ts

This commit is contained in:
MacRimi
2025-11-13 17:14:47 +01:00
parent 1d47ad0c4b
commit 364e808261

View File

@@ -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<T>(endpoint: string, options?: RequestInit): Promise<T> {
const url = getApiUrl(endpoint)
const token = getAuthToken()
const headers: Record<string, string> = {
"Content-Type": "application/json",
...(options?.headers as Record<string, string>),
}
if (token) {
headers["Authorization"] = `Bearer ${token}`
}
const response = await fetch(url, {
...options,
headers: {
"Content-Type": "application/json",
...options?.headers,
},
headers,
cache: "no-store",
})