What is the difference between a reducer and middleware? : <p>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:</p>

<blockquote>
  <p>It provides a third-party extension point between dispatching an action, and the moment it reaches the reducer.</p>
</blockquote>

<p>Or:</p>

<blockquote>
  <p>Middleware is created by composing functionality that wraps separate cross-cutting concerns which are not part of your main execution task.</p>
</blockquote>

<p>But from these all I understand is that there <em>is</em> a difference, just not <em>what</em>. 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.</p>

<p>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:</p>

<pre><code>import { getAuthLevel } from './auth';

export default store =&gt; next =&gt; action =&gt; {
  return next({...action, auth: getAuthLevel(action.clientId)});
}
</code></pre>

<p>You might have a bunch of users like this:</p>

<pre><code>{
users: [{
  clientId: 'bobsUnqiueClientId',
  user: 'Bob',
  password: 'ilikecats',
  authlevel: 'OVERLORD'
}, {
  clientId: 'anotherUniqueClientId',
  user: 'Alice',
  password: 'boblikescats',
  authlevel: 'MINION'
}]}
</code></pre>

<p>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.</p>

<p>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?</p>
