mirror of
https://github.com/vladmandic/sdnext.git
synced 2026-01-27 15:02:48 +03:00
21 lines
503 B
JavaScript
21 lines
503 B
JavaScript
let user = null;
|
|
let token = null;
|
|
|
|
async function authFetch(url, options = {}) {
|
|
if (!token) {
|
|
const res = await fetch(`${window.subpath}/token`);
|
|
if (res.ok) {
|
|
const data = await res.json();
|
|
user = data.user;
|
|
token = data.token;
|
|
}
|
|
}
|
|
if (user && token) {
|
|
if (!options.headers) options.headers = {};
|
|
const encoded = btoa(`${user}:${token}`);
|
|
options.headers.Authorization = `Basic ${encoded}`;
|
|
}
|
|
const res = await fetch(url, options);
|
|
return res;
|
|
}
|