未调用护照 jwt 验证回调 [英] passport jwt verify callback not called

查看:111
本文介绍了未调用护照 jwt 验证回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用passport-jwt 包进行简单的身份验证,令牌由jsonwebtoken 生成.但问题是从未调用过验证回调.

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.

这是我的passport.js代码.

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);
            }
        });

    }));
};

希望收到您的来信.

谢谢

推荐答案

问题是你应该添加 'JWT '(JWT 和原始 jwt 签名前的一个空格).请查看本教程 http://blog.slatepeak.com/building-a-basic-restful-api-for-a-chat-system/约书亚寻求帮助.顺便说一句,请确保您的 findOne 中是否需要where".

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.

这篇关于未调用护照 jwt 验证回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆