Skip to content

Instantly share code, notes, and snippets.

@rob-mcgrail
Last active September 28, 2017 01:39
Show Gist options
  • Save rob-mcgrail/e306888262efe8d1a47d060c882d12dd to your computer and use it in GitHub Desktop.
Save rob-mcgrail/e306888262efe8d1a47d060c882d12dd to your computer and use it in GitHub Desktop.
import axios from 'axios';
import config from 'src/config';
export default axios.create({
baseURL: config.apiBaseUrl,
timeout: 3000,
headers: {
'content-type': 'application/json'
}
});
import { createStore, applyMiddleware, compose } from 'redux';
import { createLogger } from 'redux-logger';
import thunk from 'redux-thunk';
import syncAxiosToken from './syncAxiosToken';
import ApplicationReducer from './reducers';
export default function (initialState = {}) {
const loggerMiddleware = createLogger();
const middleware = [thunk, loggerMiddleware];
const store = createStore(
ApplicationReducer,
initialState,
compose(
applyMiddleware(...middleware)
)
);
syncAxiosToken(store);
return store;
}
import watch from 'redux-watch';
import axios from 'src/axios';
export default (store) => {
const w = watch(store.getState, 'auth.authentication');
store.subscribe(w((token) => {
const authorization = token ? `Bearer ${token}` : '';
axios.defaults.headers.common.Authorization = authorization;
}));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment