mikenicholson / passport-jwt

Passport authentication using JSON Web Tokens
MIT License
1.96k stars 213 forks source link

Passport strategy not being called at all #173

Closed joshk132 closed 5 years ago

joshk132 commented 5 years ago

I seem to not be able to call my JWT passport strategy at all, the reason that makes me think it is not calling it is that I am getting no output from a console.log which I would expect to run every time no matter whether the token is valid or not. Heck even if there was no token I think it should run.

Here is an example route of mine, keep in mind I am using controllers and that the method I am using has been used for both my login as well as sign up local strategies.

router.get('/auth/test', 
  sessions.authorize,
  (req, res) => {
    res.send('You reached a private route.');
  });

Here is that controller/middleware I mentioned before

exports.authorize = (req, res, next) => {
    passport.authenticate('jwt', { 
        session: false
    }, (err, token) => {
        if (err) console.log(err);
        next();
    })(req, res, next);
};

And finally my passport strategy. I have tried a few different opts.jwtFromRequest that I found here and on StackOverflow none of which worked so I went back to this one.

var opts = {};
opts.jwtFromRequest = ExtractJWT.fromAuthHeaderAsBearerToken();
opts.secretOrKey = config.passport.jwtSecret;
passport.use(new JwtStrategy(opts, function(jwt_payload, done) {
  console.log('payload received: ', jwt_payload);
    User.findOneById({id: jwt_payload.id}, function(err, user) {
      console.log('User: ', user);
      if (user) {
        console.log(user);
        return done(null, user);
      }
      if (err) {
        return done(err, false);
      }
    });
}));

I am using Postman with the authorization type of Bearer token and I am have tried just the plain token as well as doing JWT <token> with the space as well as JWT in lower case too.

I'm not sure what else might be helpful, hopefully someone can help as I have been at this for a few days now.

joshk132 commented 5 years ago

It seems like it is skipping my sessions.authorize, totally. I was expecting to at the least see the output from console.log('payload received: ', jwt_payload); but nothing from that shows.

If I put passport.authenticate('jwt', {session: false}), in my route instead of sessions.authorize I get a 401 with a valid token and also no output from that console.log

mikenicholson commented 5 years ago

Have you been able to solve you issue? This doesn't sound like an issue with passport-jwt, but a broader issue you are having with passport or your application.

skourismanolis commented 5 years ago

I also have the same issue, I even copy-pasted the code from here and I got the same issue, not even the console.log from the JwtStrategy function is running.

mikenicholson commented 5 years ago

Closing as the original author has not responded.