Send "Authorization: Bearer …" on API requests.

We need this because we might have more recent credentials than
those in the cookie store.
This commit is contained in:
D. Berge
2024-05-01 10:15:26 +02:00
parent ea8ea12429
commit 76a90df768

View File

@@ -1,5 +1,5 @@
async function api ({state, commit, dispatch}, [resource, init = {}, cb]) {
async function api ({state, getters, commit, dispatch}, [resource, init = {}, cb]) {
try {
commit("queueRequest");
if (init && init.hasOwnProperty("body")) {
@@ -9,10 +9,17 @@ async function api ({state, commit, dispatch}, [resource, init = {}, cb]) {
"Content-Type": "application/json"
}
}
}
if (!init.headers) {
init.headers = {};
}
// We also send Authorization: Bearer …
if (getters.jwt) {
init.headers["Authorization"] = "Bearer "+getters.jwt;
}
if (typeof init.body != "string") {
init.body = JSON.stringify(init.body);
}
}
const url = /^https?:\/\//i.test(resource) ? resource : (state.apiUrl + resource);
const res = await fetch(url, init);
if (typeof cb === 'function') {