Add token injector to better handle urls
This commit is contained in:
parent
b6577eaba9
commit
7a33821d19
4 changed files with 20 additions and 3 deletions
|
@ -8,7 +8,7 @@ export const LOGIN = makeAsyncActionSet('LOGIN');
|
|||
export function login(token, callback) {
|
||||
return buildAsyncAction(
|
||||
LOGIN,
|
||||
endpoints.get('USER_INFO').query({ 'circle-token': token }).toString(),
|
||||
endpoints.get('USER_INFO').query({ 'circle-token': token }),
|
||||
methods.GET,
|
||||
{},
|
||||
{ token, callback }
|
||||
|
|
17
app/middleware/tokenInjector.js
Normal file
17
app/middleware/tokenInjector.js
Normal file
|
@ -0,0 +1,17 @@
|
|||
import { CALL_API, isRSAA } from 'redux-api-middleware';
|
||||
|
||||
export default store => next => action => {
|
||||
if (!isRSAA(action)) {
|
||||
return next(action);
|
||||
}
|
||||
|
||||
const TOKEN = store.getState().user.get('token');
|
||||
console.log('GOT TOKEN', TOKEN);
|
||||
if (TOKEN) {
|
||||
action[CALL_API].endpoint = action[CALL_API].endpoint.query('circle-token', TOKEN );
|
||||
console.log('SET URL TO', action[CALL_API].endpoint.toString());
|
||||
}
|
||||
|
||||
action[CALL_API].endpoint = action[CALL_API].endpoint.toString(); // COnvert url object to string
|
||||
return next(action);
|
||||
};
|
|
@ -2,9 +2,7 @@ import * as actions from '../actions';
|
|||
import { Map } from 'immutable';
|
||||
|
||||
export default function user(state = Map(), action) {
|
||||
console.log(action.type);
|
||||
if (action.type === actions.LOGIN.SUCCESS) {
|
||||
console.log('SUCCESS!', action.meta.token);
|
||||
return Map(action.payload).set('token', action.meta.token);
|
||||
}
|
||||
return state;
|
||||
|
|
|
@ -8,6 +8,7 @@ import filter from 'redux-storage-decorator-filter';
|
|||
|
||||
import { apiMiddleware } from 'redux-api-middleware';
|
||||
import actionChain from './middleware/actionChain';
|
||||
import tokenInjector from './middleware/tokenInjector';
|
||||
|
||||
const engine = filter(createEngine(settings.get('STORAGE_KEY')));
|
||||
|
||||
|
@ -18,6 +19,7 @@ export default function configureStore(initialState) {
|
|||
const load = storage.createLoader(engine);
|
||||
|
||||
const createStoreWithMiddleware = applyMiddleware(
|
||||
tokenInjector,
|
||||
apiMiddleware,
|
||||
actionChain,
|
||||
storeMiddleware // Last in list
|
||||
|
|
Reference in a new issue