egig egig - 4 months ago 13
Node.js Question

Jsonwebtoken verify always return only {iat: xxx }

According to documentation, https://github.com/auth0/node-jsonwebtoken#jwtverifytoken-secretorpublickey-options-callback,

jwt.verify
will returns decode payload, I run the simple script:

var token = jwt.sign({email: req.body.email,}, 's3cr3t');
var decoded = jwt.verify(token, 's3cr3t');
console.log(decoded)


but it only output like:
{ iat: 1470725598 }


I expect the output should be like
{email: myemail@domain.com,}


Is there something I am missing ?

Answer

I tried to replicate your issue on my machine but I was able to. But as soon as I tried to mimic a scenario where yourreq.body.email property was undefined.

Example:

var jwt = require('jsonwebtoken');
var token = jwt.sign({email: undefined}, 's3cr3t');
var decoded = jwt.verify(token, 's3cr3t'); 

Then it outputs;

{ iat: 1470727340 }

Which matches your issue. So I suspect your issue was just due to req.body.email being undefined.

Whereas assuming req.body.email is correctly set to "myemail@domain.com" then the output would be;

{ email: 'myemail@domain.com', iat: 1470727500 }

Also, just a side note. You might want to wrap the .verify method inside a try-catch clause as written in the documentation, verify throws an error when a token is invalid.