title: Passport-jwt Unauthorized
body: <p>I'm New to node js. I'm using passport jwt for authentication. When i tried to authenticate, its always showing "unauthorized".</p>
  
  <p>my <em>passport.js</em> file</p>
  
  <pre><code>const JwtStrategy = require('passport-jwt').Strategy;
  const ExtractJwt = require('passport-jwt').ExtractJwt;
  const User = require('../models/user');
  const config = require('../config/database');
  
  module.exports = function(passport){
    let opts = {};
    opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
    opts.secretOrKey = config.secret;
    passport.use(new JwtStrategy(opts, (jwt_payload, done) =&gt; {
  
      User.getUserById(jwt_payload._doc._id, (err, user) =&gt; {
        if(err){
          return done(err, false);
        }
  
        if(user){
          return done(null, user);
        } else {
          return done(null, false);
        }
      });
    }));
  }
  </code></pre>
  
  <p>user model user.js</p>
  
  <pre><code>module.exports.getUserById = function(id, callback){
    User.findById(id, callback);
  }
  </code></pre>
  
  <p>routes</p>
  
  <pre><code>router.get('/profile', passport.authenticate('jwt', {session:false}), (req, res, next) =&gt; {
    res.json({user: req.user});
  });
  </code></pre>
  
  <p>When I google it many suggested to change this line in passport.js</p>
  
  <pre><code>User.getUserById(jwt_payload._doc._id, (err, user) =&gt; {
  </code></pre>
  
  <p>I tried with</p>
  
  <pre><code>User.getUserById(jwt_payload._id, (err, user) =&gt; {
  User.findById(jwt_payload._id, (err, user) =&gt; {
  </code></pre>
  
  <p>still now i'm getting this same error.</p>