Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import axios from 'axios';
- import { getBasePath, Storage } from 'react-jhipster';
- // import { Storage } from 'react-jhipster';
- // function getBasePath() {
- // return 'http://friendscorp.ddns.net:8080';
- // }
- const TIMEOUT = 1000000; // 10000
- const setupAxiosInterceptors = onUnauthenticated => {
- const onRequestSuccess = config => {
- const token = Storage.local.get('jhi-authenticationToken') || Storage.session.get('jhi-authenticationToken');
- if (token) {
- config.headers.Authorization = `Bearer ${token}`;
- }
- config.timeout = TIMEOUT;
- config.url = `${getBasePath().replace(/\/$/, '')}${config.url}`;
- return config;
- };
- const onResponseSuccess = response => response;
- const onResponseError = err => {
- console.log(err);
- const status = err.status || err.response.status;
- if (status === 403 || status === 401) {
- onUnauthenticated();
- }
- return Promise.reject(err);
- };
- axios.interceptors.request.use(onRequestSuccess);
- axios.interceptors.response.use(onResponseSuccess, onResponseError);
- };
- export default setupAxiosInterceptors;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement