Search code examples
node.jsjwtjwt.io

Always getting invalid signature in jwt.io


I always get invalid signature when I input the generated token in jwt.io Here is my code for making the token

const secret = 'secret';
const token = jwt.sign({
    username: user.username,
    userID: user._id
  },
  secret, {
    expiresIn: "1hr"
  }
);

What did I do wrong?

I'm using the jsonwebtoken package. https://github.com/auth0/node-jsonwebtoken


Solution

  • If you are using jsonwebtoken lib, I tried and able to create the token and verify as well. Please have a look at the code and let me know in comments if you are still facing the issue.

    var jwt = require('jsonwebtoken')
    
    const secret = 'secret';
    const token = jwt.sign({
            username: "",
            userID: 1
        },
        secret, {
            expiresIn: "1hr"
        },
        function(err, token) {
            if (err) {
                console.log(err);
            } else {
                console.log(token);
            }
        });
    

    Here is the link of jwt.io where I entered your secret used and it's saying verified.

    https://jwt.io/#debugger-io?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IiIsInVzZXJJRCI6MSwiaWF0IjoxNTI4NTUyMDYyLCJleHAiOjE1Mjg1NTU2NjJ9.raL79zTGONyXgr9vuXzAyMflHJ0JqPYTXsy9KwmlXhA

    enter image description here