Hi,
I am experimenting with this:
Your custom middleware can intercept this action to dispatch new actions in response to URL changes.
I have written a barebones middleware for routes with auth in their result. Like so:
export const auth = store => next => action => {
if (
action.type === LOCATION_CHANGED &&
action.payload &&
action.payload.result &&
action.payload.result.auth
) {
const loggedIn = selectors.isLoggedIn(store.getState());
if (!loggedIn) {
setTimeout(() => {
store.dispatch({
type: PUSH,
payload: '/login'
});
// Other dispatch which works perfectly
// store.dispatch(fetchVocabsIfNeeded());
}, 3000);
}
}
return next(action);
};
The setTimeout is just to simulate an async fetch that I would usually dispatch to the server to see if I had an active session. If not, then I want to go to the login page.
This seems to generate a ROUTER_PUSH action with a payload of '/login'. The route does not change and a fragment for '/login' is not displayed. However, if I dispatch that same action from an onClick on my App component, the result is a ROUTER_LOCATION_CHANGE with the payload of pathname, key, route, params, etc, and it works fine.
I am not experienced with redux middleware and suspect that this has something to do with the middleware chain and where redux-little-router sits in that chain, but I'm unsure how to address this. Should this be possible?
Cheers
Hi,
I am experimenting with this:
I have written a barebones middleware for routes with
authin theirresult. Like so:The
setTimeoutis just to simulate an async fetch that I would usually dispatch to the server to see if I had an active session. If not, then I want to go to the login page.This seems to generate a
ROUTER_PUSHaction with a payload of '/login'. The route does not change and a fragment for '/login' is not displayed. However, if I dispatch that same action from anonClickon myAppcomponent, the result is aROUTER_LOCATION_CHANGEwith the payload ofpathname,key,route,params, etc, and it works fine.I am not experienced with redux middleware and suspect that this has something to do with the middleware chain and where
redux-little-routersits in that chain, but I'm unsure how to address this. Should this be possible?Cheers