Crud api header
How can i add a auth token to the header that i call if i use infencer
axios. You can create an instance of axios and set the Authorization header with the token. This axios instance can then be passed to your dataProvider.localStorage or any other place where you store it.Authorization header of API calls. Here's an example:user.tokenaxios.interceptors.request.useimport axios from "axios";
const axiosInstance = axios.create();
// This sets the authorization headers on Axios instance
axiosInstance.defaults.headers.common = {
Authorization: `Bearer ${user.token}`,
};
// Pass the axios instance to the dataProvider
dataProvider={dataProvider(API_URL, axiosInstance)}axiosInstance.interceptors.request.use((config) => {
// Retrieve the token from local storage
const token = JSON.parse(localStorage.getItem("auth"));
// Check if the header property exists
if (config.headers) {
// Set the Authorization header if it exists
config.headers["Authorization"] = `Bearer ${token}`;
}
return config;
});