0
0
Expressframework~10 mins

JWT token creation in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - JWT token creation
Receive user data
Prepare payload
Use secret key
Call jwt.sign()
Generate token string
Send token to user
This flow shows how user data is turned into a JWT token using a secret key and the jwt.sign() function.
Execution Sample
Express
const jwt = require('jsonwebtoken');

const payload = { id: 123, name: 'Alice' };
const secret = 'mysecretkey';

const token = jwt.sign(payload, secret);
console.log(token);
This code creates a JWT token from user data and a secret key, then prints the token string.
Execution Table
StepActionInputOutputNotes
1Prepare payload{ id: 123, name: 'Alice' }Payload object readyUser data collected
2Set secret key'mysecretkey'Secret key string readyUsed to sign token
3Call jwt.sign()payload, secretJWT token string generatedToken encodes payload and signature
4Print tokentoken stringToken string output to consoleToken looks like three base64 parts
5EndN/AProcess completeToken ready to send to user
💡 Token creation completes after jwt.sign() returns the token string
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
payloadundefined{ id: 123, name: 'Alice' }{ id: 123, name: 'Alice' }{ id: 123, name: 'Alice' }{ id: 123, name: 'Alice' }
secretundefinedundefined'mysecretkey''mysecretkey''mysecretkey'
tokenundefinedundefinedundefinedeyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Key Moments - 3 Insights
Why do we need a secret key when creating a JWT token?
The secret key is used to sign the token so that the server can verify it later. Without it, anyone could create fake tokens. See step 2 and 3 in the execution_table.
What does the jwt.sign() function output?
It outputs a string token that contains encoded user data and a signature. This token is what we send to the user. See step 3 and 4 in the execution_table.
Is the payload data visible in the token?
Yes, the payload is encoded (not encrypted) inside the token, so anyone can decode it but cannot change it without the secret. See step 3 output.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'token' after step 3?
AA JWT token string
BThe secret key string
CThe payload object
DUndefined
💡 Hint
Check the 'Output' column at step 3 in the execution_table.
At which step is the secret key set in the variable_tracker?
AAfter Step 1
BAfter Step 2
CAfter Step 3
DFinal
💡 Hint
Look at the 'secret' row in variable_tracker and see when it changes from undefined.
If we change the secret key, what will happen to the token?
AThe token will stay the same
BThe payload will change
CThe token will be different because the signature changes
DThe token will be invalid immediately
💡 Hint
Think about how the secret key affects the signature in jwt.sign() at step 3.
Concept Snapshot
JWT token creation in Express:
- Prepare a payload object with user data
- Use a secret key string
- Call jwt.sign(payload, secret) to create token
- Token is a string encoding data + signature
- Send token to user for authentication
Full Transcript
This visual trace shows how to create a JWT token in Express. First, user data is prepared as a payload object. Then a secret key string is set. The jwt.sign() function is called with the payload and secret, which returns a token string. This token contains encoded user data and a signature to verify authenticity. Finally, the token is printed or sent to the user. Variables like payload, secret, and token change step-by-step as shown. Key points include the importance of the secret key for signing and the token being a string output. This process is essential for secure user authentication in web apps.