首页 文章

JWT令牌问题和护照w /节点js

提问于
浏览
2

我一直在尝试使用护照和护照-jwt来测试受保护的路线 .

当用户尝试登录并在Postman中进行测试时,我已经达到了生成令牌的程度 .

我已经创建了一个路由并作为一个参数传递了passport.authenticate与jwt策略,并在整个地方收到错误 .

在我的主server.js中,我需要护照:

passport = require('passport');

app.use(passport.initialize());

// passport config
require('./config/passport')(passport);

我的文件夹结构是这样的:
enter image description here

在我的护照配置文件中,我有这个:

const jwtStrategy =  require('passport-jwt').Strategy;
const ExtractJwt = require('passport-jwt').ExtractJwt;
const mongoose = require('mongoose');
const User = mongoose.model('users')
const keys = require('../config/keys');

const opts = {};
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = keys.secretOrKey;

module.export = passport => {
    passport.use(
        new jwtStrategy(opts, (jwt_payload, done) => {
        console.log(jwt_payload);
    }));
};

我的路线是这样的:

// @route get to /users/current
// @desc: return the current user (who the jwt token belongs to)
// @access: should be public
router.get('/current', 
    passport.authenticate('jwt', { session: false }), 
    (req, res) => {
        res.json({msg: "Success"})
    }
);

我似乎无法通过的第一个错误是在控制台中:

require('./config/passport')(passport);
                            ^

TypeError: require(...) is not a function

在邮递员中,当我尝试转到/ users / current并传递确认的持票人令牌时,我得到了这个:

错误:未知的身份验证策略"jwt"
在尝试

1 回答

  • 1

    在护照配置文件中你有错字 module.export 实际上它的 module.exports 这就是为什么在要求之后它没有将它作为功能重新认识

相关问题