小编eve*_*Bit的帖子

jsonwebtoken中的有效负载错误

我正在使用nodejs和angular cli创建一个Web应用程序我正在使用JWT来验证我的登录功能.但是当我处理它时抛出了这个错误

错误:期望"有效负载"成为普通对象.在验证(D:\ Mean_Projects\meanauthapp \node_modules\jsonwebtoken\sign.js:34:11)在object.module.exports上的validatePayload(D:\ Mean_Projects\meanauthapp \node_modules\jsonwebtoken\sign.js:56:10) [as sign](D:\ Mean_Projects\meanauthapp \node_modules\jsonwebtoken\sign.js:108:7)位于bcrypt.compare的User.comparePassword(D:\ Mean_Projects\meanauthapp\routes\users.js:86:27) D:\ Mean_Projects\meanauthapp \node_modules\bcryptjs\dist\bcrypt.js:(D:\ Mean_Projects\meanauthapp\dist\bcrypt.js:1353:21 at Immediate.allxt [as _onImmediate](D:\ Mean_Projects\meanauthapp \node_modules\bcryptjs\dist\bcrypt.js:1233:21)at runCallback(timers.js:785:20) )在processImmediate的tryOnImmediate(timers.js:747:5)[as _immediateCallback](timers.js:718:5)

这是我的护照密码

    const JwtStrategy= require('passport-jwt').Strategy;
    const ExtractJwt=require('passport-jwt').ExtractJwt;
    const User= require('../models/user');
    const config=require('../config/database');        
    module.exports=function(passport){
    let opts={};
    opts.jwtFromRequest=ExtractJwt.fromAuthHeader();
    opts.secretOrKey=config.secret;
    opts.issuer = 'accounts.examplesoft.com';
    opts.audience = 'yoursite.net';
    passport.use(new JwtStrategy(opts,(jwt_payload,done)=>{
        console.log(jwt_payload);
        User.getUserById(jwt_payload._doc._id,(err,user)=>{
            if(err){
                return done(err,false);
            }
            if(user){
                return done(null,user);
            }
            else{
                return done(null,false);
            }
        });
    }));
}
Run Code Online (Sandbox Code Playgroud)

我的验证和获取配置文件的代码

// Authenticate
router.post('/authenticate', (req, res, next) => {
  const username = req.body.username;
  const …
Run Code Online (Sandbox Code Playgroud)

javascript node.js jwt passport.js angular

20
推荐指数
2
解决办法
1万
查看次数

标签 统计

angular ×1

javascript ×1

jwt ×1

node.js ×1

passport.js ×1