oswilno/crates/web-assets/assets/app.js

33 lines
956 B
JavaScript
Raw Normal View History

2023-08-01 16:29:03 +02:00
import './elements/oswilno-price.js';
2023-08-04 16:32:10 +02:00
import './elements/oswilno-error.js';
2023-08-03 16:16:46 +02:00
import("https://unpkg.com/htmx.org@1.9.4/dist/htmx.min.js");
2023-08-05 22:20:23 +02:00
2023-08-15 12:33:53 +02:00
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');
}
2023-08-05 22:20:23 +02:00
if (status === 422 || status === 400) {
2023-08-15 12:33:53 +02:00
detail.shouldSwap = true;
detail.isError = false;
}
});
body.addEventListener('htmx:configRequest', function (evt) {
if (localStorage.getItem('jwt')) {
evt.detail.headers.Authorization = 'Bearer ' + (localStorage.getItem('jwt') || '');
2023-08-05 22:20:23 +02:00
}
});