32 lines
938 B
JavaScript
32 lines
938 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) {
|
|
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') || '');
|
|
}
|
|
});
|
|
|