title: Passport Jwt Unauthorized
body: <p>Hello I was trying to implement passport-jwt on my webapp. But It wont work I've seen several same post and tried what they did but on my case it didnt worked.</p>
  
  <p>here are some <a href="https://stackoverflow.com/questions/35528377/authenticating-node-api-with-passport-jwt">Authenticating node API with passport-jwt</a></p>
  
  <p><a href="https://stackoverflow.com/questions/18690354/passport-local-strategy-not-getting-called">passport local strategy not getting called</a></p>
  
  <p><a href="https://stackoverflow.com/questions/45858758/passport-jwt-unauthorized">Passport-jwt Unauthorized</a></p>
  
  <p>I've tried those and it didnt worked.
  Here is my code on my passport.js</p>
  
  <pre><code>    var config = require('./config');
  
  var JwtStrategy = require('passport-jwt').Strategy,
      ExtractJwt = require('passport-jwt').ExtractJwt;
  module.exports =
      function(passport){
          var opts ={};
          opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
          opts.secretOrKey = config.secret;
              passport.use(new JwtStrategy(opts,function(jwt_payload,done){
                  console.log('HERE');
                  console.log(jwt_payload);
                  User.findOne({id:jwt_payload._id},function(err,user){
                      if(err)
                          return done(err,false);
                      if(user)
                          return done(null,user);
                      else
                          return done(null,false);
                          })
  
                      }));
              };
  </code></pre>
  
  <p>It wont even reach the <code>console.log('HERE');</code></p>
  
  <p>and this is how I generate my token</p>
  
  <pre><code>router.post('/login',function(req,res,next){
      var username = req.body.username;
      var password = req.body.password;
      User.findUser(username,function(err,user){
          if(err) return res.json({success:false,msg:"Error"});
          if(!user) return res.json({success:false,msg:"No Such user"});
          //res.json(user);
          console.log(user);
          if(user){
              User.comparePass(password,user.password,function(err,IsMatched){
                  if(err) return res.json({success:false,msg:'error});
                  if(!IsMatched) return  res.json({success:false,msg:"wrong password"});
                  if(IsMatched){
                      var token = jwt.sign(user.toObject(),config.secret,{expiresIn:604800});
                      res.json({success:true,token:'JWT '+ token,
                              user:{
                                  name:user.name,
                                  username:user.username,
                                  password:user.password,
                                  email:user.email,
                                  contact:user.contact,
                                  address:user.address
                              }
                      });
  
                  }
              });
          }
      });
  });
  </code></pre>
  
  <p>and here is what im trying to access</p>
  
  <pre><code>router.get('/profile',passport.authenticate('jwt',{session:false}), function(req, res, next) {
    res.json({user:req.user});
  });
  </code></pre>
  
  <p>Thanks!</p>