passport jwt verify callback not called

元气小坏坏 提交于 2019-12-12 15:03:43

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/38833470/passport-jwt-verify-callback-not-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!