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
✗ Incorrect
The Payload contains the data or claims, such as user ID or roles.
Which method from 'jsonwebtoken' is used to create a JWT token?
Asign()
Bverify()
Cdecode()
Dencrypt()
✗ Incorrect
The sign() method creates a JWT token by encoding the payload and signing it.
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
✗ Incorrect
Using the wrong secret key causes verification to fail and throws an error.
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
✗ Incorrect
The Signature ensures the token was not tampered with and is from a trusted source.
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' }
✗ Incorrect
The 'expiresIn' option sets how long the token is valid, e.g., '2h' for 2 hours.
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.
Practice
(1/5)
1. What is the main purpose of a JWT token in a Node.js application?
easy
A. To securely transmit user identity without sending passwords repeatedly
B. To store user passwords in the database
C. To encrypt all user data on the server
D. To create a permanent session without expiration
Solution
Step 1: Understand JWT token purpose
JWT tokens are used to prove user identity securely without sending passwords every time.
Step 2: Compare options
Only To securely transmit user identity without sending passwords repeatedly correctly describes this purpose; others describe unrelated or incorrect uses.
Final Answer:
To securely transmit user identity without sending passwords repeatedly -> Option A
Quick Check:
JWT purpose = Secure identity proof [OK]
Hint: JWT = identity proof without password resend [OK]
Common Mistakes:
Thinking JWT stores passwords
Confusing JWT with encryption of all data
Assuming JWT creates permanent sessions
2. Which of the following is the correct syntax to generate a JWT token using the jsonwebtoken library in Node.js?
easy
A. jwt.generate(payload, secretKey, { expiresAt: '1h' })
B. jwt.create(payload, secretKey, { expiresIn: '1h' })
C. jwt.sign(payload, secretKey, { expiresIn: '1h' })
D. jwt.make(payload, secretKey, { expire: '1h' })
Solution
Step 1: Recall jsonwebtoken method for token creation
The correct method to create a token is jwt.sign with payload, secret, and options.
Step 2: Check option syntax
Only jwt.sign(payload, secretKey, { expiresIn: '1h' }) uses jwt.sign and correct option key expiresIn; others use invalid method names or option keys.
Final Answer:
jwt.sign(payload, secretKey, { expiresIn: '1h' }) -> Option C
Quick Check:
Token creation method = jwt.sign [OK]
Hint: Use jwt.sign() with expiresIn option for token creation [OK]
Step 1: Check token creation and verification secrets
Token is signed with 'mysecret' but verified with 'wrongsecret', causing verification failure.
Step 2: Understand jwt.verify behavior on secret mismatch
jwt.verify throws an error if secret does not match, so decoded.id is not accessible.
Final Answer:
Secret key used for verification is incorrect -> Option D
Quick Check:
Secret mismatch = verification error [OK]
Hint: Verify with same secret as sign to avoid errors [OK]
Common Mistakes:
Assuming token expired without expiration set
Thinking payload lacks id property
Misusing jwt.sign method syntax
5. You want to generate a JWT token that expires in 30 minutes and includes the user's email and role. Which code snippet correctly does this using the jsonwebtoken library?
Step 1: Identify correct payload and expiration option
The payload includes email and role, and expiration uses expiresIn with '30m' for 30 minutes.
Step 2: Validate option keys and values
Only jwt.sign({ email: user.email, role: user.role }, 'secretKey', { expiresIn: '30m' }) uses correct option key expiresIn and valid time format '30m'; others use invalid keys or formats.