I have created an axios instance:
const instance = axios.create({
baseURL: 'https://example.io/api/',
timeout: 1000
});
and would like to use it on different components. My webapp is secured with Keycloak and every request that goes to API, needs an authentication token. To get the token, I call the Keycloak method as follows:
kc
.init({onLoad: "login-required"})
.then(authenticated => {
if (kc.idToken === undefined) {
return Promise.reject("Can not be authenticated")
}
return authenticated && root !== null ? start({authToken: kc.idToken}, root) : Promise.reject("Can not be authenticated")
})
.catch(console.log)
When I do the request to the API server, I pass the token as Bearer token in the request header. To avoid passing the token on every request, can I use the intercepter right or what do I have to do?