From 76a90df768dcf337e633f4f5b556f49cd0adcb25 Mon Sep 17 00:00:00 2001 From: "D. Berge" Date: Wed, 1 May 2024 10:15:26 +0200 Subject: [PATCH] =?UTF-8?q?Send=20"Authorization:=20Bearer=20=E2=80=A6"=20?= =?UTF-8?q?on=20API=20requests.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We need this because we might have more recent credentials than those in the cookie store. --- .../source/src/store/modules/api/actions.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/www/client/source/src/store/modules/api/actions.js b/lib/www/client/source/src/store/modules/api/actions.js index 6be81d9..459f308 100644 --- a/lib/www/client/source/src/store/modules/api/actions.js +++ b/lib/www/client/source/src/store/modules/api/actions.js @@ -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,9 +9,16 @@ async function api ({state, commit, dispatch}, [resource, init = {}, cb]) { "Content-Type": "application/json" } } - if (typeof init.body != "string") { - init.body = JSON.stringify(init.body); - } + } + 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);