Search code examples
node.jsrestful-authenticationpassport.js

passport jwt verify callback not called


I'm using passport-jwt package for simple authentication and the token is generated by jsonwebtoken. But the problem is that verify callback is never called.

Here my passport.js code.

var JwtStrategy = require('passport-jwt').Strategy;
var User = require('../app/models/user');
var config = require('../config/database'); 
var opts = {};
opts.jwtFromRequest = function(req) {
    var token = null;
    if (req && req.headers) {
        token = req.headers.authorization;
    }
    return token;
};
opts.secretOrKey = config.secret;
console.log(opts);
module.exports = function(passport) {
    passport.use(new JwtStrategy(opts, function(jwt_payload, done) {
        User.findOne({_id: jwt_payload._doc._id}, function(err, user) {
            if (err) {
              return done(err, false);
            }
            if (user) {
                done(null, user);
            } else {
              done(null, false);
            }
        });

    }));
};

Hope to hear from you.

Thanks


Solution

  • The problem is that you should add 'JWT '(JWT and a space ahead of the original jwt signed). Please check this tutorial http://blog.slatepeak.com/building-a-basic-restful-api-for-a-chat-system/ by Joshua for assistance. By the way, make sure that if you need a 'where' inside your findOne or not.