-
Notifications
You must be signed in to change notification settings - Fork 0
/
customAxios.js
44 lines (36 loc) · 1018 Bytes
/
customAxios.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import axios from "axios";
import {store} from "../redux/store";
//const urlDev = 'http://localhost:3000'; // MUST CHANGE
function getUserToken(){
return store.getState().user.token || 0;
}
function getAuthorizationHeader(){
const token = getUserToken();
if(token)
return `Bearer ${token}`
else
return '';
}
export function customAxios(){
const axiosInstance = axios.create({
baseURL: urlDev,
timeout: 5000,
});
/* Request */
axiosInstance.interceptors.request.use(req => {
const tokenAuth = getAuthorizationHeader();
if(tokenAuth !== ''){
req.headers.Authorization = tokenAuth;
}
return req;
});
/* Response */
axiosInstance.interceptors.response.use(function(response){
/* On success = status code 200 */
return response.data;
}, function(error){
/* On failure = status code <> 200 */
return Promise.reject(error);
});
return axiosInstance;
}