Identify Users with JWT

Share this video with your friends

Send Tweet
Published 2 years ago
Updated 2 years ago

Now that we have our encoded JWT we need to be able to decode it!

The jsonwebtoken library makes this very easy. All we have to do is pass the jwt and our token to a decoder function. In that function definition we pass the arguments into the verify method and return the result.

Instructor: [0:00] Create a decoded variable that stores the return value of an async function named verifyJWT. This function takes the token to be decoded and the secrets the token was encoded with.

[0:16] Log the decoded value to the console for inspection. Create the verified JWT function and call the verify method from the JSON Web Token library, passing it to JWT and the token secret.

[0:33] Head to the browser and click the Authorize link. Then head to the terminal, and you should see that the decoded values have been printed.

[0:44] If the verification threw an error, it means that either the JWT is invalid, has been messed with, or you have supplied a wrong secret. You can confirm this by passing a random string instead of the token as a secret. Click the Authorize link again, and you should get an error in the console.