Separated user and auth stores

This commit is contained in:
Jori Lallo
2017-05-29 19:08:03 -07:00
parent 98a5283e27
commit 0d87d6abf5
12 changed files with 191 additions and 143 deletions

View File

@ -1,5 +1,6 @@
// @flow
import _ from 'lodash';
import invariant from 'invariant';
import stores from 'stores';
type Options = {
@ -39,9 +40,9 @@ class ApiClient {
Accept: 'application/json',
'Content-Type': 'application/json',
});
if (stores.user.authenticated) {
// $FlowFixMe this is not great, need to refactor
headers.set('Authorization', `Bearer ${stores.user.token}`);
if (stores.auth.authenticated) {
invariant(stores.auth.token, 'JWT token not set properly');
headers.set('Authorization', `Bearer ${stores.auth.token}`);
}
// Construct request
@ -62,15 +63,10 @@ class ApiClient {
return response;
}
// // Handle 404
// if (response.status === 404) {
// // return browserHistory.push('/404');
// }
// // Handle 401, log out user
// if (response.status === 401) {
// return stores.user.logout();
// }
// Handle 401, log out user
if (response.status === 401) {
return stores.auth.logout(() => (window.location = '/'));
}
// Handle failed responses
const error = {};
@ -112,5 +108,4 @@ class ApiClient {
export default ApiClient;
// In case you don't want to always initiate, just import with `import { client } ...`
const client = new ApiClient();
export { client };
export const client = new ApiClient();