{"size": 832, "ext": "js", "max_stars_repo_path": "src/api/client.js", "max_stars_repo_name": "Monkhjargal/credit-frontend", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/api/client.js", "max_issues_repo_name": "Monkhjargal/credit-frontend", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/api/client.js", "max_forks_repo_name": "Monkhjargal/credit-frontend", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 21.3333333333, "max_line_length": 67, "alphanum_fraction": 0.625}
import axios from "axios";
import config from "../config";
import { storage } from "../utils";

axios.defaults.auth = config.auth;

const setAuthorizationHeader = (token = null) => {
  if (token) {
    axios.defaults.headers.common.Authorization = token;
  } else {
    delete axios.defaults.headers.common.Authorization;
  }
};

axios.interceptors.response.use(
  function(response) {
    return response;
  },

  function(error) {
    const cant = error.config.url.indexOf("/login") !== -1;

    if (cant) {
      return Promise.reject(error);
    }

    if (!cant && error.response && error.response.status === 401) {
      setTimeout(() => {
        storage.remove("user");
        window.location.href = "/";
      }, 2000);
    }

    return Promise.reject(error);
  }
);

export { axios as default, setAuthorizationHeader };
