Files
automatic/javascript/authWrap.js
T
vladmandic 175201a72f fix progress auth
Signed-off-by: vladmandic <mandic00@live.com>
2025-12-02 10:38:17 +01:00

26 lines
643 B
JavaScript

let user = null;
let token = null;
async function getToken() {
if (!token || !user) {
const res = await fetch(`${window.subpath}/token`);
if (res.ok) {
const data = await res.json();
user = data.user;
token = data.token;
}
}
return { user, token };
}
async function authFetch(url, options = {}) {
const { localUser, localToken } = await getToken();
if (localUser && localToken) {
if (!options.headers) options.headers = {};
const encoded = btoa(`${localUser}:${localToken}`);
options.headers.Authorization = `Basic ${encoded}`;
}
const res = await fetch(url, options);
return res;
}