From 7a33821d19f407da98d5795f92fc4933b6ee9be4 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Thu, 5 May 2016 13:34:41 +0100 Subject: [PATCH] Add token injector to better handle urls --- app/actions/index.js | 2 +- app/middleware/tokenInjector.js | 17 +++++++++++++++++ app/reducers/user.js | 2 -- app/store.js | 2 ++ 4 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 app/middleware/tokenInjector.js diff --git a/app/actions/index.js b/app/actions/index.js index 0b3f6f1..9f41565 100644 --- a/app/actions/index.js +++ b/app/actions/index.js @@ -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 } diff --git a/app/middleware/tokenInjector.js b/app/middleware/tokenInjector.js new file mode 100644 index 0000000..ab01bd4 --- /dev/null +++ b/app/middleware/tokenInjector.js @@ -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); +}; diff --git a/app/reducers/user.js b/app/reducers/user.js index f48a65d..25d6035 100644 --- a/app/reducers/user.js +++ b/app/reducers/user.js @@ -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; diff --git a/app/store.js b/app/store.js index e3fb29d..da7dc23 100644 --- a/app/store.js +++ b/app/store.js @@ -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