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) {
|
export function login(token, callback) {
|
||||||
return buildAsyncAction(
|
return buildAsyncAction(
|
||||||
LOGIN,
|
LOGIN,
|
||||||
endpoints.get('USER_INFO').query({ 'circle-token': token }).toString(),
|
endpoints.get('USER_INFO').query({ 'circle-token': token }),
|
||||||
methods.GET,
|
methods.GET,
|
||||||
{},
|
{},
|
||||||
{ token, callback }
|
{ 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';
|
import { Map } from 'immutable';
|
||||||
|
|
||||||
export default function user(state = Map(), action) {
|
export default function user(state = Map(), action) {
|
||||||
console.log(action.type);
|
|
||||||
if (action.type === actions.LOGIN.SUCCESS) {
|
if (action.type === actions.LOGIN.SUCCESS) {
|
||||||
console.log('SUCCESS!', action.meta.token);
|
|
||||||
return Map(action.payload).set('token', action.meta.token);
|
return Map(action.payload).set('token', action.meta.token);
|
||||||
}
|
}
|
||||||
return state;
|
return state;
|
||||||
|
|
|
@ -8,6 +8,7 @@ import filter from 'redux-storage-decorator-filter';
|
||||||
|
|
||||||
import { apiMiddleware } from 'redux-api-middleware';
|
import { apiMiddleware } from 'redux-api-middleware';
|
||||||
import actionChain from './middleware/actionChain';
|
import actionChain from './middleware/actionChain';
|
||||||
|
import tokenInjector from './middleware/tokenInjector';
|
||||||
|
|
||||||
const engine = filter(createEngine(settings.get('STORAGE_KEY')));
|
const engine = filter(createEngine(settings.get('STORAGE_KEY')));
|
||||||
|
|
||||||
|
@ -18,6 +19,7 @@ export default function configureStore(initialState) {
|
||||||
const load = storage.createLoader(engine);
|
const load = storage.createLoader(engine);
|
||||||
|
|
||||||
const createStoreWithMiddleware = applyMiddleware(
|
const createStoreWithMiddleware = applyMiddleware(
|
||||||
|
tokenInjector,
|
||||||
apiMiddleware,
|
apiMiddleware,
|
||||||
actionChain,
|
actionChain,
|
||||||
storeMiddleware // Last in list
|
storeMiddleware // Last in list
|
||||||
|
|
Reference in a new issue