I'm having a little trouble understanding the difference of application between a reducer and middleware. A number of sites describe middleware even giving precise definitions:
It provides a third-party extension point between dispatching an action, and the moment it reaches the reducer.
Or:
Middleware is created by composing functionality that wraps separate cross-cutting concerns which are not part of your main execution task.
But from these all I understand is that there is a difference, just not what. From what I can tell, the difference is that one takes an action and passes that action on, and the other takes an action and a state and "passes the state on". But you still have access to the store in the midddleware. So store and action both go through the middleware and then reducers. So a reducer could perform logging.
While logging seems like an obvious application for middleware, there are more ambiguous examples. For example, writing in some trivial authentication into a module, you could use a middleware function to take an action sent by a user and determine their level of authentication:
import { getAuthLevel } from './auth';
export default store => next => action => {
return next({...action, auth: getAuthLevel(action.clientId)});
}
You might have a bunch of users like this:
{
users: [{
clientId: 'bobsUnqiueClientId',
user: 'Bob',
password: 'ilikecats',
authlevel: 'OVERLORD'
}, {
clientId: 'anotherUniqueClientId',
user: 'Alice',
password: 'boblikescats',
authlevel: 'MINION'
}]}
Then you might have actions that correspond to different authentication levels. You could map that using middleware, but you'd need to know more specific details about the actions going through and it seems like more "execution-related" code. But then it doesn't need to know anything about the state. It just needs to decide which actions to forward on to the reducers.
So? Would such code go in a reducer or in middleware? And can anyone provide other specific examples that clarify the difference between the two?