0
0
Expressframework~10 mins

Refresh token concept in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Refresh token concept
User logs in
Server issues Access Token + Refresh Token
User uses Access Token to access resources
Access Token expires?
NoContinue Access
Yes
User sends Refresh Token to server
Server verifies Refresh Token
If valid, server issues new Access Token
User continues with new Access Token
If Refresh Token invalid, user must log in again
This flow shows how a user gets tokens, uses the access token until it expires, then uses the refresh token to get a new access token without logging in again.
Execution Sample
Express
app.post('/token', (req, res) => {
  const refreshToken = req.body.token;
  if (!refreshToken) return res.sendStatus(401);
  if (!refreshTokens.includes(refreshToken)) return res.sendStatus(403);
  jwt.verify(refreshToken, REFRESH_SECRET, (err, user) => {
    if (err) return res.sendStatus(403);
    const accessToken = generateAccessToken({ name: user.name });
    res.json({ accessToken });
  });
});
This Express route receives a refresh token, verifies it, and if valid, sends back a new access token.
Execution Table
StepActionInputCheck/ConditionResult/Output
1Receive POST /token request{ token: 'refreshToken123' }Is token present?Yes, proceed
2Check if refresh token is in store'refreshToken123' in refreshTokens?YesProceed to verify
3Verify refresh token signaturejwt.verify(refreshToken123)Valid?Yes, decoded user info
4Generate new access tokenUser info from tokenN/ANew access token created
5Send response{ accessToken: 'newAccessToken456' }N/AClient receives new access token
6If token missing or invalidN/ANo or invalid tokenSend 401 or 403 status
💡 Stops when refresh token is missing or invalid, or after sending new access token.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
refreshTokenundefined'refreshToken123''refreshToken123''refreshToken123''refreshToken123'undefined
refreshTokens['refreshToken123']['refreshToken123']['refreshToken123']['refreshToken123']['refreshToken123']['refreshToken123']
userundefinedundefinedundefined{ name: 'Alice' }{ name: 'Alice' }undefined
accessTokenundefinedundefinedundefinedundefined'newAccessToken456''newAccessToken456'
Key Moments - 3 Insights
Why do we check if the refresh token is in the store before verifying it?
Because a refresh token might be revoked or logged out, so checking the store (refreshTokens array) ensures only valid tokens are accepted before verifying signature (see execution_table step 2).
What happens if the refresh token is expired or tampered with?
The jwt.verify call fails and returns an error, so the server responds with status 403, stopping the process (see execution_table step 3).
Why do we send a new access token but not a new refresh token?
Access tokens expire quickly for security; refresh tokens last longer. We only issue a new access token here to keep the session alive without forcing login again (see execution_table step 4 and 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result at step 3 when jwt.verify fails?
AA new access token is generated
BThe refresh token is added to the store
CThe server sends status 403
DThe user is logged out automatically
💡 Hint
Check execution_table row with Step 3 and the condition 'Valid?'
At which step does the server check if the refresh token exists in the stored list?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Look at execution_table row where it checks 'refreshToken123 in refreshTokens?'
If the refresh token was missing in the request body, what would happen?
AThe server sends status 401
BThe server generates a new access token anyway
CThe server adds a new refresh token to the store
DThe server ignores the request
💡 Hint
See execution_table step 1 condition 'Is token present?' and result
Concept Snapshot
Refresh Token Concept in Express:
- User logs in and gets Access + Refresh Tokens
- Access Token used for resource access, expires quickly
- When expired, client sends Refresh Token to /token endpoint
- Server verifies Refresh Token and issues new Access Token
- Refresh Token stored server-side to allow revocation
- If Refresh Token invalid or missing, user must log in again
Full Transcript
This visual execution trace shows how refresh tokens work in an Express app. When a user logs in, the server sends both an access token and a refresh token. The user uses the access token to access protected resources. When the access token expires, the user sends the refresh token to the server's /token route. The server first checks if the refresh token is present and stored in the server's list. Then it verifies the token's signature. If valid, the server generates a new access token and sends it back. If the refresh token is missing, invalid, or expired, the server responds with an error status, forcing the user to log in again. This flow helps keep users logged in securely without asking for credentials repeatedly.