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

43 lines
1.4 KiB
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-09-12 18:03:42 +02:00
import './elements/oswilno-parking-space.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
const READ_AUTH_HEADER = 'Authorization';
2023-08-28 14:57:31 +02:00
const AUTH_HEADER = 'ACX-Authorization';
const REFRESH_HEADER = 'ACX-Refresh';
2023-08-15 12:33:53 +02:00
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;
if (status === 200) {
const bearer = xhr.getResponseHeader(READ_AUTH_HEADER);
2023-08-15 12:33:53 +02:00
if (bearer) {
console.log(xhr);
2023-08-15 12:33:53 +02:00
localStorage.setItem('jwt', bearer.replace(/^Bearer /i, ''));
}
2023-08-28 14:57:31 +02:00
const refresh = xhr.getResponseHeader(REFRESH_HEADER);
if (refresh) {
localStorage.setItem('refresh', bearer.replace(/^Bearer /i, ''));
}
2023-08-15 12:33:53 +02:00
} 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) {
2023-09-27 18:17:27 +02:00
if (!localStorage.getItem('jwt')) {
return;
2023-08-05 22:20:23 +02:00
}
2023-09-27 18:17:27 +02:00
evt.detail.headers[AUTH_HEADER] = 'Bearer ' + (localStorage.getItem('jwt') || '');
evt.detail.headers[READ_AUTH_HEADER] = 'Bearer ' + (localStorage.getItem('jwt') || '');
evt.detail.headers[REFRESH_HEADER] = (localStorage.getItem('refresh') || '');
2023-08-05 22:20:23 +02:00
});