title: How to create a middleware in Koa.js using ES6
body: <p>I am building an application in Koa.JS for the first time after coming from a recent background of using Express.</p>
  
  <p>I am using Babel to transpile the code at runtime whilst developing giving me the beneift of wirting ES6+ code. </p>
  
  <p>I am having issues in Koa defining a middleware that I want to use and store on the ctx object to be used later in my application. I am not sure whereI am going wrong as I am able to do similar in Express (albeing not in ES6 code).</p>
  
  <p>Here is my middleware:</p>
  
  <pre><code>const Config = require('../../Config');
  
  import jwt from 'jsonwebtoken';
  
  const JWTController = () =&gt; {
      return {
          async generateToken(tokenVars) {
              const secretKey = Config.auth.secret;
              const claims = {
                  sub: tokenVars.userid,
                  iss: Config.auth.issuer,
                  account: tokenVars.accountId,
                  permissions: ''
              };
  
              let token = await jwt.sign(claims, secretKey);
              return token;
          },
  
          async decodeClaims(token) {
              const decodedToken = jwt.decode( token, {complete: true} );
              return decodedToken;
          }
      }
  };
  
  export default JWTController;
  </code></pre>
  
  <p>And the section of my app.js entry file where I use the middleware:</p>
  
  <pre><code>});
  mongoose.connection.on('error', (err) =&gt; {
      console.log(err);
  });
  
  app.use(bodyParser());
  app.use(serve(appRoot + '/../client'));
  app.use(serve(appRoot + '/../client/assets'));
  
  app.use((ctx, next) =&gt; {
      ctx.JWTController = JWTController;
      next();
  });
  </code></pre>
  
  <p>Can anyone see why this is not working?</p>
  
  <p>Thanks</p>