title: create a custom passport-jwt strategy middleware callback
body: <p>I want to create a custom middleware for passport-jwt to handle authentication.</p>
  
  <p>here is what I have done to create my own middleware : </p>
  
  <pre><code>var models  = require('../models');
  var passport = require("passport");  
  var passportJWT = require("passport-jwt");  
  var config = require("../config/config.json");  
  var ExtractJwt = passportJWT.ExtractJwt;  
  var Strategy = passportJWT.Strategy;  
  var params = {  
      secretOrKey: config.jwtSecret,
      jwtFromRequest: ExtractJwt.fromAuthHeader()
  };
  
  /**
   * jwt authentication strategy
   */
  var strategy = new Strategy(params, function(payload, done) {
      models.User.findById(payload.id)
      .then((user)=&gt;{
          if (user) {
              return done(null, {
                  id: user.id,
                  username : user.username
              });
          } else {
              return done(new Error("User not found"), false);
          }
      }).catch((err)=&gt;{
          return done(err, false);
      });
  });
  passport.use(strategy);
  
  module.exports =  {  
      initialize: function() {
          return passport.initialize();
      },
      authenticate: (req, res, next)=&gt;{
          passport.authenticate('jwt', { session: false }, (err, user, info)=&gt;{ 
              if (err) { return next(err); } 
              if (!user) { return res.send("Custom Unauthorised").end(); } 
              // edit as per comment
              //return res.send("Test Route Accessed").end();
              req.user = user;   // Forward user information to the next middleware
              next();
          })(req, res, next);
      }
  };
  </code></pre>
  
  <p>but everytime I type 'npm start' to run the app I face this error : </p>
  
  <p>if (request.headers[AUTH_HEADER]) {
             ^
  TypeError: Cannot read property 'headers' of undefined.</p>
  
  <p>the authorization header is set in the request.</p>