/* global localStorage */
import axios from 'axios';

export const BASE_URL = process.env.REACT_APP_API_URL;

const request = (endpoint, { headers = {}, body, ...otherOptions }, method) => {
  return axios(`${BASE_URL}${endpoint}`, {
    ...otherOptions,
    headers: {
      ...headers,
      Authorization: `Bearer ${localStorage.getItem('jwt.token')}}`,
      Accept: 'application/json',
      'Content-Type': 'application/json',
    },
    data: body ? JSON.stringify(body) : undefined,
    method,
  });
};

export const api = {
  get(endpoint, options = {}) {
    return request(endpoint, options, 'get');
  },
  post(endpoint, options = {}) {
    return request(endpoint, options, 'post');
  },
};

export default api;

@@ -3,28 +3,22 @@
  global 
-localStorage
+window
  */%0Aimpo
@@ -103,17 +103,200 @@
 %0A%0Aconst 
-r
+getToken = () =%3E %7B%0A  return window.localStorage.getItem('jwt.token');%0A%7D;%0A%0Aconst setToken = (%7B newToken %7D) =%3E %7B%0A  window.localStorage.setItem('jwt.token', newToken);%0A%7D;%0A%0Aconst abstractR
 equest =
@@ -493,42 +493,18 @@
 r $%7B
-localStorage.getItem('jwt.t
+getT
 oken
-')%7D
+()
 %7D%60,%0A
@@ -649,16 +649,16 @@
 method,%0A
-
   %7D);%0A%7D;
@@ -655,24 +655,920 @@
 ,%0A  %7D);%0A%7D;%0A%0A
+const checkForRefreshToken = (endpoint, content, method) =%3E (error) =%3E %7B%0A  if (error.response && error.response.status === 401) %7B%0A    return abstractRequest('/auth/refresh', %7B%7D, 'post').then((%7B data %7D) =%3E %7B%0A      setToken(data);%0A%0A      return abstractRequest(endpoint, content, method);%0A    %7D);%0A  %7D%0A  return Promise.reject(error);%0A%7D;%0A%0Aconst checkForRelogin = error =%3E %7B%0A  if (!error.response %7C%7C !error.response.data %7C%7C window.location.pathname === '/login') %7B%0A    return Promise.reject(error);%0A  %7D%0A%0A  const message = error.response.data.error;%0A%0A  if (%5B'token_expired', 'token_invalid', 'token_not_provided'%5D.includes(message)) %7B%0A    window.location = '/login';%0A  %7D%0A%0A  return Promise.reject(error);%0A%7D;%0A%0Aconst request = (endpoint, content, method) =%3E %7B%0A  return abstractRequest(endpoint, content, method)%0A    .catch(checkForRefreshToken(endpoint, content, method))%0A    .catch(checkForRelogin);%0A%7D;%0A%0A
 export const

Refresh JWT token if expires