Trying to do a get request to an endpoint that requires an Auth token. But whenever the request is made - it returns a 401 error ("Authorization header is expected'). The token is retrieved asynchronously from chrome.storage. But doesn't seem to be sent in the ajax request. Any help is appreciated.
ajax: {
type: "GET",
beforeSend: async function (request) {
var token = await bearerToken();
request.setRequestHeader("Authorization", token);
},
url: function (params) {
return getChannelURL(params.term);
},
dataType: "json",
data: function (response) {
// data -> entire json object
return response;
},
// data.data -> array containing all the channel elements
processResults: function (data, params) {
for (var i = 0; i < data.data.length; i++) {
data.data[i].text = data.data[i].text || data.data[i].name;
}
return {
results: data.data,
};
},
cache: true,
},
minimumInputLength: 1,
placeholder: "Select a Channel",
width: "resolve",
templateResult: formatChannelData,
});
Below is the function that stores the token
bearerToken = async () => {
var tokenRetrieved = await helper.getLocalStorage('tokenElement')
var token = tokenRetrieved.tokenElement
return ("Bearer "+ token);
}