Files
automatic/javascript/authWrap.js
T
vladmandic fb2f9ea650 new server info panel
Signed-off-by: vladmandic <mandic00@live.com>
2026-03-18 15:37:57 +01:00

33 lines
832 B
JavaScript

let user;
let token;
async function getToken() {
if (token === undefined || user === undefined) {
const res = await fetch(`${window.subpath}/token`);
if (res.ok) {
const data = await res.json();
user = data.user;
token = data.token;
log('getToken', user);
}
}
return { user, token };
}
async function authFetch(url, options = {}) {
await getToken();
if (user && token) {
if (!options.headers) options.headers = {};
const encoded = btoa(`${user}:${token}`);
options.headers.Authorization = `Basic ${encoded}`;
}
let res;
try {
res = await fetch(url, options);
if (!res.ok) error('fetch', { status: res?.status || 503, url, user, token });
} catch (err) {
error('fetch', { status: res?.status || 503, url, user, token, error: err });
}
return res;
}