fix(web): skip empty Authorization header for forward-auth proxy compatibility

Centralize duplicated getToken() + Bearer header logic into a single
formatHeaders() helper in lib/api.ts. When no token exists, the
Authorization header is omitted entirely instead of sending an empty
Bearer token, which breaks forward-auth proxies like Authelia behind
Caddy.

Changes:
- Add formatHeaders() with try-catch around localStorage access
- Replace 20+ duplicated getToken() definitions across tool components
- Migrate all call sites including file-details, settings, change-password
- Update tests to verify header omission on empty token

Based on the fix proposed by @jules2689 in #6, with improvements:
file placement (lib/api.ts vs components), localStorage error handling,
simplified truthiness check, and complete call-site coverage.

Co-Authored-By: Julian Nadeau <julian@jnadeau.ca>
This commit is contained in:
Siddharth Kumar Sah
2026-04-05 18:41:06 +08:00
co-authored by Julian Nadeau
parent f21579c7a3
commit d0c69d6a46
26 changed files with 104 additions and 177 deletions
@@ -23,7 +23,7 @@ import {
X,
} from "lucide-react";
import { useCallback, useEffect, useMemo, useState } from "react";
import { apiDelete, apiGet, apiPost, apiPut, clearToken } from "@/lib/api";
import { apiDelete, apiGet, apiPost, apiPut, clearToken, formatHeaders } from "@/lib/api";
import { cn, copyToClipboard } from "@/lib/utils";
import { GemLogo } from "../common/gem-logo";
@@ -287,10 +287,9 @@ function SystemSection() {
const formData = new FormData();
formData.append("file", file);
try {
const token = localStorage.getItem("stirling-token");
await fetch("/api/v1/settings/logo", {
method: "POST",
headers: { Authorization: `Bearer ${token}` },
headers: formatHeaders(),
body: formData,
});
setSettings((prev) => ({ ...prev, customLogo: "true" }));