久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

jsonwebtoken 中的有效負載錯誤

Payload error in jsonwebtoken(jsonwebtoken 中的有效負載錯誤)
本文介紹了jsonwebtoken 中的有效負載錯誤的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在使用 nodejs 和 angular cli 制作一個 Web 應用程序我正在使用 JWT 來驗證我的登錄功能.但是當我處理它時拋出了這個錯誤

I am making a web application using nodejs and angular cli I'm using JWT to authenticate my login function . But when I process it threw this error

錯誤:預期的有效負載"是一個普通對象.驗證時 (D:Mean_Projectsmeanauthapp ode_modulesjsonwebtokensign.js:34:11)在 validatePayload (D:Mean_Projectsmeanauthapp ode_modulesjsonwebtokensign.js:56:10)在 Object.module.exports [作為符號] (D:Mean_Projectsmeanauthapp ode_modulesjsonwebtokensign.js:108:7)在 User.comparePassword (D:Mean_Projectsmeanauthapp outesusers.js:86:27)在 bcrypt.compare (D:Mean_Projectsmeanauthappmodelsuser.js:53:9)在 D:Mean_Projectsmeanauthapp ode_modulescryptjsdistcrypt.js:297:21在 D:Mean_Projectsmeanauthapp ode_modulescryptjsdistcrypt.js:1353:21在 Immediate.next [as _onImmediate] (D:Mean_Projectsmeanauthapp ode_modulescryptjsdistcrypt.js:1233:21)在 runCallback (timers.js:785:20)在 tryOnImmediate (timers.js:747:5)在 processImmediate [as _immediateCallback] (timers.js:718:5)

Error: Expected "payload" to be a plain object. at validate (D:Mean_Projectsmeanauthapp ode_modulesjsonwebtokensign.js:34:11) at validatePayload (D:Mean_Projectsmeanauthapp ode_modulesjsonwebtokensign.js:56:10) at Object.module.exports [as sign] (D:Mean_Projectsmeanauthapp ode_modulesjsonwebtokensign.js:108:7) at User.comparePassword (D:Mean_Projectsmeanauthapp outesusers.js:86:27) at bcrypt.compare (D:Mean_Projectsmeanauthappmodelsuser.js:53:9) at D:Mean_Projectsmeanauthapp ode_modulescryptjsdistcrypt.js:297:21 at D:Mean_Projectsmeanauthapp ode_modulescryptjsdistcrypt.js:1353:21 at Immediate.next [as _onImmediate] (D:Mean_Projectsmeanauthapp ode_modulescryptjsdistcrypt.js:1233:21) at runCallback (timers.js:785:20) at tryOnImmediate (timers.js:747:5) at processImmediate [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);
            }
        });
    }));
}

我的身份驗證和獲取配置文件代碼

My code for authenticate and get profile

// Authenticate
router.post('/authenticate', (req, res, next) => {
  const username = req.body.username;
  const password = req.body.password;

  User.getUserByUsername(username, (err, user) => {
    if(err) throw err;
    if(!user){
      return res.json({success: false, msg: 'User not found'});
    }

    User.comparePassword(password, user.password, (err, isMatch) => {
      if(err) throw err;
      if(isMatch){
        const token = jwt.sign(user, config.secret, {
          expiresIn: 604800 // 1 week
        });

        res.json({
          success: true,
          token: 'JWT '+token,
          user: {
            id: user._id,
            name: user.name,
            username: user.username,
            email: user.email
          }
        });
      } else {
        return res.json({success: false, msg: 'Wrong password'});
      }
    });
  });
});

// Profile
router.get('/profile', passport.authenticate('jwt', {session:false}), (req, res, next) => {
  res.json({user: req.user});
});

推薦答案

在線失敗

const token = jwt.sign(user, config.secret, {

出現錯誤預期有效負載"是一個普通對象"

您的 user 對象在此處初始化:

Your user object is initialized here:

User.getUserByUsername(username, (err, user)

我假設是 mongoosejs 對象,它包含許多方法并且不是可序列化的".您可以通過使用 mongoose 中的 .lean() 或普通 toJSON 方法傳遞一個普通對象來處理這個問題:

Which I assume is mongoosejs object, which contains many methods and is not "serializable". You could handle this by passing a plain object, by either using .lean() from mongoose or plain toJSON method:

const token = jwt.sign(user.toJSON(), config.secret, {
  expiresIn: 604800 // 1 week
});

這篇關于jsonwebtoken 中的有效負載錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Is Math.random() cryptographically secure?(Math.random() 在密碼學上是安全的嗎?)
Secure random numbers in javascript?(在javascript中保護隨機數?)
How to avoid multiple token refresh requests when making simultaneous API requests with an expired token(使用過期令牌發出同時 API 請求時如何避免多個令牌刷新請求)
JWT not decoding quot;JWT malformedquot; - Node Angular(JWT 未解碼“JWT malformed;- 節點角度)
How to invalidate a JWT token with no expiry time(如何使沒有到期時間的 JWT 令牌無效)
Authorization header in img src link(img src 鏈接中的授權標頭)
主站蜘蛛池模板: 精品美女在线观看视频在线观看 | 高清黄色 | 国产一级精品毛片 | 久久天天躁狠狠躁夜夜躁2014 | 国产福利91精品 | 午夜小影院 | 国产精品18毛片一区二区 | 在线免费观看黄色 | 免费观看一级特黄欧美大片 | 99热首页 | 国产精品国色综合久久 | 久久久www成人免费精品 | 中文字幕 欧美 日韩 | 免费激情 | 亚洲精品久久久久久久久久久久久 | 日韩av在线免费 | 国产一级淫片a直接免费看 免费a网站 | 日韩在线不卡视频 | 国产精品精品视频 | 天堂一区二区三区 | 亚洲一区二区三区免费观看 | 欧美一级二级视频 | 久久中文字幕一区 | 久久精品亚洲欧美日韩精品中文字幕 | 欧美999 | 久久久噜噜噜www成人网 | 不卡视频在线 | 日韩久久精品视频 | 久久午夜国产精品www忘忧草 | 国产精品亚洲成在人线 | 国产午夜精品一区二区三区在线观看 | 精品一级毛片 | 日韩at| 精品一区二区电影 | av国产精品| 国产成人精品久久二区二区91 | 精品在线一区二区 | 日韩国产精品一区二区三区 | 成年人国产在线观看 | 久久精品视频在线免费观看 | 狠狠做深爱婷婷综合一区 |