Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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: [email protected],}

Is there something I am missing ?

like image 361
egig Avatar asked Aug 09 '16 07:08

egig


People also ask

What does Jsonwebtoken verify return?

jwt.verify(token, secretOrPublicKey, [options, callback]) (Synchronous) If a callback is not supplied, function acts synchronously. Returns the payload decoded if the signature is valid and optional expiration, audience, or issuer are valid. If not, it will throw the error.

What is IAT in JWT verify?

iat" (Issued At) Claim The "iat" (issued at) claim identifies the time at which the JWT was issued. This claim can be used to determine the age of the JWT. Its value MUST be a number containing a NumericDate value. Use of this claim is OPTIONAL.

What is the use of Jsonwebtoken in node JS?

JWTs are mainly used for authentication. After a user signs in to an application, the application then assigns JWT to that user. Subsequent requests by the user will include the assigned JWT. This token tells the server what routes, services, and resources the user is allowed to access.

What is Jsonwebtoken package?

JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object.


1 Answers

I was not able to mimic your problem until I set the property req.body.email to undefined.

Example:

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

With it been undefined, the output would look like this;

{ iat: 1470727340 }

and this matches exactly what you were having which cause me to suspect your main issue was just with the property req.body.email been undefined.

Assuming req.body.email is correctly set to "[email protected]" then the output would be;

{ email: '[email protected]', iat: 1470727500 }

Just a side note here. You might want to consider wrapping the .verify method inside a try-catch clause, as shown in the documentation. This is useful for verifying and throwing error when a token is invalid.

like image 170
Samuel Toh Avatar answered Oct 02 '22 09:10

Samuel Toh