mirror of
https://github.com/vladmandic/automatic
synced 2026-08-27 23:51:00 +02:00
175201a72f
Signed-off-by: vladmandic <mandic00@live.com>
26 lines
643 B
JavaScript
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;
|
|
}
|