fix(htmx): add Authorization header to all HTMX requests

- Include gb-access-token in Authorization header for all HTMX requests
- Add X-Session-ID header for session tracking
- Fix 401 errors on all HTMX-powered API calls
This commit is contained in:
Rodrigo Rodriguez (Pragmatismo) 2026-01-10 14:00:43 -03:00
parent ab8c7ae02b
commit b5c75e650b

View file

@ -21,11 +21,27 @@
htmx.config.defaultSettleDelay = 100; htmx.config.defaultSettleDelay = 100;
htmx.config.timeout = 10000; htmx.config.timeout = 10000;
// Add CSRF token to all requests if available // Add CSRF token and Authorization header to all requests
document.body.addEventListener("htmx:configRequest", (event) => { document.body.addEventListener("htmx:configRequest", (event) => {
const token = localStorage.getItem("csrf_token"); const csrfToken = localStorage.getItem("csrf_token");
if (token) { if (csrfToken) {
event.detail.headers["X-CSRF-Token"] = token; event.detail.headers["X-CSRF-Token"] = csrfToken;
}
// Add Authorization header with access token
const accessToken =
localStorage.getItem("gb-access-token") ||
sessionStorage.getItem("gb-access-token");
if (accessToken) {
event.detail.headers["Authorization"] = `Bearer ${accessToken}`;
}
// Add session ID if available
const sessionId =
localStorage.getItem("gb-session-id") ||
sessionStorage.getItem("gb-session-id");
if (sessionId) {
event.detail.headers["X-Session-ID"] = sessionId;
} }
}); });