Pass JWT token to dataprovider

How can I pass the jwt token to the simple-rest dataprovider when making a request?
2 Replies
kapa.ai
kapa.ai5w ago
Hi @BlackBearFTW. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To pass a JWT token to the simple-rest data provider when making a request in refine, you can create a custom axios instance and configure it to include the JWT token in the Authorization header for every request. Here is a step-by-step guide on how to achieve this: 1. Create a custom axios instance: - First, create an axios instance and set up an interceptor to add the JWT token to the Authorization header. 2. Update the dataProvider to use the custom axios instance: - Pass the custom axios instance to the dataProvider. Here is an example of how you can do this:
// Import axios
import axios from "axios";

// Create a custom axios instance
const axiosInstance = axios.create({
baseURL: "<https://api.com",> // Replace with your API URL
});

// Function to add token to requests
axiosInstance.interceptors.request.use(
(config) => {
// Retrieve the token from wherever it's stored
const token = localStorage.getItem('access_token');
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
},
(error) => {
return Promise.reject(error);
}
);

// Update the dataProvider to use the custom axios instance
const dataProvider = simpleRestDataProvider("<https://api.com",> axiosInstance);

// Use the dataProvider in your Refine component
<Refine dataProvider={dataProvider} />
// Import axios
import axios from "axios";

// Create a custom axios instance
const axiosInstance = axios.create({
baseURL: "<https://api.com",> // Replace with your API URL
});

// Function to add token to requests
axiosInstance.interceptors.request.use(
(config) => {
// Retrieve the token from wherever it's stored
const token = localStorage.getItem('access_token');
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
},
(error) => {
return Promise.reject(error);
}
);

// Update the dataProvider to use the custom axios instance
const dataProvider = simpleRestDataProvider("<https://api.com",> axiosInstance);

// Use the dataProvider in your Refine component
<Refine dataProvider={dataProvider} />
This setup ensures that the JWT token is included in the Authorization header for every request made by the simple-rest data provider. For more information, you can refer to the discussion on GitHub (https://github.com/refinedev/refine/discussions/5880) where a similar solution is provided. If you need further details, you can also check the documentation (https://refine.dev/docs/data/packages/simple-rest/#url-design) for the simple-rest package. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5w ago
<@490961337896402944> kapa.ai is still learning and improving, please let us know how it did by reacting below