Files
test/public/api.js
Ali 1d539e44ac
Some checks failed
Build & Push Image / build-image (push) Has been cancelled
auth
2026-02-24 21:49:23 +09:00

20 lines
601 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
export async function authFetch(url, options = {}) {
const token = localStorage.getItem("token"); // hol den JWT aus localStorage
// füge Headers zusammen: ContentType + Bearer Token
const headers = {
"Content-Type": "application/json",
...options.headers, // vorhandene Header nicht überschreiben
...(token ? {
"Authorization": `Bearer ${token}` // 🔑 wichtiger Header
} : {})
};
// führe den fetch aus mit dem zusammengebauten Header
const response = await fetch(url, {
...options,
headers
});
return response; // response weitergeben
}