Skip to content

Instantly share code, notes, and snippets.

@hugoalmeidahh
Forked from diego3g/api.js
Created June 23, 2021 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hugoalmeidahh/aad5709a14baf2123ec73123ef0bb91d to your computer and use it in GitHub Desktop.
Save hugoalmeidahh/aad5709a14baf2123ec73123ef0bb91d to your computer and use it in GitHub Desktop.
import axios from 'axios';
import store from '~/store';
const api = axios.create({
baseURL: 'http://localhost:3333',
});
api.interceptors.request.use((config) => {
const { token } = store.getState().auth; // Aqui poderia ser localStoraget.getItem
const headers = { ...config.headers };
if (token) {
headers.Authorization = `Bearer ${token}`;
}
return { ...config, headers };
});
export default api;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment