0
0
Node.jsframework~5 mins

JWT token generation and verification in Node.js - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a JWT token?
A JWT (JSON Web Token) is a compact, URL-safe token used to securely transmit information between parties as a JSON object. It is often used for authentication and authorization.
Click to reveal answer
beginner
What are the three parts of a JWT token?
A JWT token has three parts separated by dots: Header, Payload, and Signature. The Header describes the token type and algorithm, the Payload contains the data, and the Signature verifies the token's integrity.
Click to reveal answer
beginner
Which Node.js library is commonly used for JWT token generation and verification?
The 'jsonwebtoken' library is commonly used in Node.js to create and verify JWT tokens easily.
Click to reveal answer
intermediate
How do you generate a JWT token in Node.js using 'jsonwebtoken'?
Use the sign() method from 'jsonwebtoken' with a payload and a secret key. Example: jwt.sign({ userId: 123 }, 'secretKey', { expiresIn: '1h' }) creates a token valid for 1 hour.
Click to reveal answer
intermediate
How do you verify a JWT token in Node.js?
Use the verify() method from 'jsonwebtoken' with the token and the secret key. It checks the token's signature and expiration, returning the decoded payload if valid or throwing an error if not.
Click to reveal answer
What does the 'Payload' part of a JWT token contain?
AThe token expiration time only
BThe token's signature
CThe encryption algorithm used
DThe data or claims about the user
Which method from 'jsonwebtoken' is used to create a JWT token?
Asign()
Bverify()
Cdecode()
Dencrypt()
What happens if you verify a JWT token with the wrong secret key?
AVerification fails and an error is thrown
BThe token is accepted anyway
CThe token expires immediately
DThe payload is changed
What is the purpose of the Signature in a JWT token?
ATo set the token expiration
BTo store user data
CTo verify the token's integrity and authenticity
DTo encrypt the payload
Which of these is a valid option to set token expiration when generating a JWT?
A{ timeout: 120 }
B{ expiresIn: '2h' }
C{ expireAfter: 3600 }
D{ validFor: '1d' }
Explain how to generate and verify a JWT token in Node.js using the 'jsonwebtoken' library.
Think about the two main methods: sign and verify.
You got /4 concepts.
    Describe the structure of a JWT token and the role of each part.
    Remember the token has three parts separated by dots.
    You got /3 concepts.