Crema React
Search…
API Calls
We are using the Axios to make an api call. Here is the global this Axios configuration. You can find it on this path src/@crema/services/auth/jwt-auth/index.js
We are using the Axios to make an api call. Here is the global this Axios configuration. You can find it on this path src/@crema/services/auth/jwt-auth/index.js
1
import axios from "axios";
2
3
const jwtAxios = axios.create({
4
baseURL: "https://BACKEND_SERVER_ATH/api/", // YOUR_API_URL HERE
5
headers: {
6
"Content-Type": "application/json",
7
},
8
});
9
10
jwtAxios.interceptors.response.use(
11
(res) => res,
12
(err) => {
13
if (err.response && err.response.data.msg === "Token is not valid") {
14
console.log("Need to logout user");
15
}
16
return Promise.reject(err);
17
}
18
);
19
export const setAuthToken = (token) => {
20
if (token) {
21
jwtAxios.defaults.headers.common.Authorization = `Bearer ${token}`;
22
//You can define any header params like Authorization based on the your requirements
23
localStorage.setItem("token", token);
24
} else {
25
delete jwtAxios.defaults.headers.common.Authorization;
26
localStorage.removeItem("token");
27
}
28
};
29
30
export default jwtAxios;
Copied!
We are using the mock APIs in our app, to gets the fake data from the Crema fake db. We are using axios-mock-adapter to achieve this. Here is the configuration of it.
1
import jwtAxios from "../auth/jwt-auth";
2
const MockAdapter = require("axios-mock-adapter");
3
export default new MockAdapter(jwtAxios, { delayResponse: 100 });
Copied!
Copy link