oswilno/crates/web-assets/assets/app.js
2023-08-15 12:33:53 +02:00

33 lines
956 B
JavaScript

import './elements/oswilno-price.js';
import './elements/oswilno-error.js';
import("https://unpkg.com/htmx.org@1.9.4/dist/htmx.min.js");
const body = document.body;
body.addEventListener('htmx:beforeOnLoad', function (evt) {
console.log(evt);
const detail = evt.detail;
const xhr = detail.xhr;
const status = xhr.status;
const successful = detail.successful;
console.log(successful, xhr.getResponseHeader('Authorization'));
if (status === 200) {
const bearer = xhr.getResponseHeader('Authorization');
if (bearer) {
localStorage.setItem('jwt', bearer.replace(/^Bearer /i, ''));
}
} else if (status === 401) {
localStorage.removeItem('jwt');
}
if (status === 422 || status === 400) {
detail.shouldSwap = true;
detail.isError = false;
}
});
body.addEventListener('htmx:configRequest', function (evt) {
if (localStorage.getItem('jwt')) {
evt.detail.headers.Authorization = 'Bearer ' + (localStorage.getItem('jwt') || '');
}
});