Currently im having a problem fetching my menus from wordpress since the endpoint need authorization and i cant figure out how to send the authorization bearer header.
I managed to setup a afterCSR request to fetch the token.
What i setup for menus:
actions: {
theme: {
...
beforeSSR: ({ actions }) => async () => {
await actions.source.fetch("menus/2");
},
afterCSR: async ({ state, libraries, actions }) => {
if (state.frontity.platform === 'client') {
if (undefined === state.theme.token) {
actions.theme.fetchToken();
}
}
},
fetchToken
}
},
libraries: {
source: {
handlers: [menuHandler],
}
}
Handler:
const menuHandler = {
name: "menus",
priority: 10,
pattern: "menus/:id",
func: async ({ route, params, state, libraries }) => {
const { api } = libraries.source;
const { id } = params;
const response = await api.get({
endpoint: "menus/",
headers: {
"authorization": "Bearer mytokenhere",
"host": "myhosthere",
"accept": "*\/*",
},
params: {
menus: id,
per_page: 100
}
});
const items = await response.json();
const currentPageData = state.source.data[route];
Object.assign(currentPageData, {
id,
items,
isMenu: true
});
}
};
I repeatedly receive a 401 unauthorized.
Via Postman i managed to get the Menus from the endpoint so the authorization works actually. But i guess the header is not sent by frontity fetch. Any ideas on how to send the header correct?