Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import axios from 'axios';
  2. import { getBasePath, Storage } from 'react-jhipster';
  3. // import { Storage } from 'react-jhipster';
  4.  
  5. // function getBasePath() {
  6. //   return 'http://friendscorp.ddns.net:8080';
  7. // }
  8.  
  9. const TIMEOUT = 1000000; // 10000
  10. const setupAxiosInterceptors = onUnauthenticated => {
  11.   const onRequestSuccess = config => {
  12.     const token = Storage.local.get('jhi-authenticationToken') || Storage.session.get('jhi-authenticationToken');
  13.     if (token) {
  14.       config.headers.Authorization = `Bearer ${token}`;
  15.     }
  16.     config.timeout = TIMEOUT;
  17.     config.url = `${getBasePath().replace(/\/$/, '')}${config.url}`;
  18.     return config;
  19.   };
  20.   const onResponseSuccess = response => response;
  21.   const onResponseError = err => {
  22.     console.log(err);
  23.     const status = err.status || err.response.status;
  24.     if (status === 403 || status === 401) {
  25.       onUnauthenticated();
  26.     }
  27.     return Promise.reject(err);
  28.   };
  29.   axios.interceptors.request.use(onRequestSuccess);
  30.   axios.interceptors.response.use(onResponseSuccess, onResponseError);
  31. };
  32.  
  33. export default setupAxiosInterceptors;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement