Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
JWT Token Creation with Express
📖 Scenario: You are building a simple Express server that creates JSON Web Tokens (JWT) for users after they log in. JWTs help keep users logged in securely.
🎯 Goal: Build an Express server that creates a JWT token using a secret key and user data.
📋 What You'll Learn
Create an Express app variable called app
Create a secret key variable called jwtSecret with value 'mysecretkey'
Use the jsonwebtoken library to create a token with user info
Create a POST route /login that returns a JWT token
💡 Why This Matters
🌍 Real World
JWT tokens are widely used in web apps to securely keep users logged in without storing passwords on the client.
💼 Career
Understanding JWT creation is essential for backend developers working on authentication and security in web applications.
Progress0 / 4 steps
1
Setup Express app and user data
Create an Express app variable called app by requiring express and calling it. Also create a user object called user with id 1 and username 'alice'.
Express
Hint
Use const app = express() to create the app. Define user as an object with id and username.
2
Add JWT secret key
Create a constant called jwtSecret and set it to the string 'mysecretkey'.
Express
Hint
Just create a constant string variable named jwtSecret.
3
Create JWT token in /login route
Require the jsonwebtoken library as jwt. Then create a POST route /login on app. Inside the route, create a token by calling jwt.sign() with the user object and jwtSecret. Send the token as JSON with key token.
Express
Hint
Use jwt.sign(user, jwtSecret) to create the token. Send it back with res.json({ token }).
4
Start the Express server
Add code to start the Express server by calling app.listen() on port 3000. Inside the listen callback, log the message 'Server running on port 3000'.
Express
Hint
Use app.listen(3000, () => { console.log('Server running on port 3000') }) to start the server.
Practice
(1/5)
1. What is the main purpose of creating a JWT token in an Express app?
easy
A. To connect to a database
B. To style the user interface
C. To handle file uploads
D. To securely store user information for authentication
Solution
Step 1: Understand JWT token role
JWT tokens are used to safely store user data for verifying identity.
Step 2: Identify correct purpose
Among the options, only storing user info for authentication matches JWT's role.
Final Answer:
To securely store user information for authentication -> Option D
Quick Check:
JWT purpose = Authentication [OK]
Hint: JWT tokens are for authentication, not UI or database [OK]
Common Mistakes:
Confusing JWT with UI styling or database connection
Thinking JWT handles file uploads
2. Which of the following is the correct syntax to create a JWT token using the jsonwebtoken package in Express?
easy
A. jwt.generate(payload, secretKey, { expiresIn: '1h' })
B. jwt.create(payload, secretKey, { expiresIn: '1h' })
C. jwt.sign(payload, secretKey, { expiresIn: '1h' })
D. jwt.make(payload, secretKey, { expiresIn: '1h' })
Solution
Step 1: Recall jsonwebtoken method
The correct method to create a token is jwt.sign()
Step 2: Match syntax with options
Only jwt.sign(payload, secretKey, { expiresIn: '1h' }) uses jwt.sign() with payload, secretKey, and expiresIn correctly.
Final Answer:
jwt.sign(payload, secretKey, { expiresIn: '1h' }) -> Option C
Quick Check:
Token creation method = sign() [OK]
Hint: Remember: jsonwebtoken uses sign() to create tokens [OK]
Common Mistakes:
Using incorrect method names like create or generate
Omitting the expiresIn option or using wrong syntax
The secret key must be a string for signing the token securely.
Step 2: Identify error in code
The code uses 12345 (a number) as secret key, which is incorrect.
Final Answer:
Secret key should be a string, not a number -> Option A
Quick Check:
Secret key type = string [OK]
Hint: Secret key must always be a string for jwt.sign() [OK]
Common Mistakes:
Passing number instead of string as secret key
Thinking payload must be string
Believing expiresIn is invalid
Assuming callback is mandatory
5. You want to create a JWT token that expires in 30 minutes and includes the user's email and role. Which code snippet correctly achieves this in Express?
The payload must include email and role from user object.
Step 2: Use correct expiresIn format
expiresIn accepts string like '30m' for 30 minutes; number means seconds but must be a number type without quotes.
Step 3: Identify correct option
Check each: expiresAt is invalid key; expireIn is misspelled; expiresIn: 30 is only 30 seconds. Only jwt.sign({ email: user.email, role: user.role }, 'mySecret', { expiresIn: '30m' }) is correct.