Payload error in jsonwebtoken

后端 未结 7 2073
谎友^
谎友^ 2021-02-01 16:50

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

Err

7条回答
  •  花落未央
    2021-02-01 17:20

    this is clearly mentioned in the migration doc of passport-jwt

    that they have removed the ExtractJwt.fromAuthHeader() from version 2 and 3 and also to use the new method ExtractJwt.fromAuthHeaderAsBearerToken() or one of like that in place of old method. for compelte reference visit

    From your log there is the issue

    User.comparePassword (D:\Mean_Projects\meanauthapp\routes\users.js:86:27) at 
    

    so here four thing need to be updated in your code @every Bit


    First in package.json file
    Change the version to latest by using * or version no like this by going to project directory and run the command

      npm install passport-jwt --save
        "dependencies": {
        ....     
            "passport-jwt": "^3.0.1"
          }
    

    or write in the file and run the commadn

    `npm install`
        "dependencies": {
            ....     
                "passport-jwt": "*"
              }
    

    Second change this line of your code in authenticate method

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

    Third in the passport code change the old method

    ExtractJwt.fromAuthHeader();
    

    to new one, from the doc reference you need to use this method opts.jwtFromRequest=ExtractJwt.fromAuthHeaderWithScheme('jwt');

    and fourth change this

    User.getUserById(jwt_payload._id,(err,user)=>{
    

    This solution will work on latest version's


    • if you still want to use this old method then


    only change the version of your passport-jwt in package.json to 1.x.x (x is the nuber here )of your choise of lower version then 2,
    by moving to project folder and runing the command npm install
    the only thing you need to check is data in the payload_jwt,it will be inside the second layer so please check the jwt_payload.
    ok you are all set to go you had already handled User.getUserById(jwt_payload._doc._id,(err,user)=>{

提交回复
热议问题