title: Wrapping passport.authenticate *callback* inside a function doesn't work
body: <p>This is closely related to <a href="https://stackoverflow.com/questions/36327284/wrapping-passport-authenticate-inside-a-function-doesnt-work">Wrapping passport.authenticate inside a function doesn't work</a>.</p>
  
  <p>We are using <a href="http://koajs.com" rel="nofollow noreferrer">Koa</a> instead of <a href="http://expressjs.com" rel="nofollow noreferrer">Express</a>. Hence I replaced the <code>(req, res, next)</code> by <code>(ctx, next)</code>. It worked fine for the initial oauth2 call, but the callback throws an error if wrapped in a function.</p>
  
  <p><strong>Works:</strong></p>
  
  <pre><code>router.get('/auth/google/callback', passport.authenticate('google',
      {
          successRedirect: '/auth/success',
          failureRedirect: '/auth/fail',
          failureFlash: true,
      }));
  </code></pre>
  
  <p><strong>Fails:</strong></p>
  
  <pre><code>const google_callback = (ctx, next) =&gt; {
      passport.authenticate('google',
          {
              successRedirect: '/auth/success',
              failureRedirect: '/auth/fail',
              failureFlash: true,
          }
      )(ctx, next);
  };
  router.get('/auth/google/callback', google_callback);
  </code></pre>
  
  <p>The error message is:</p>
  
  <pre><code>Error: Can't set headers after they are sent.
  </code></pre>