What if your app could keep users logged in safely without bothering them again and again?
Why Refresh token concept in Express? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine a user logs into your app and you give them a token that expires quickly for security. Now, every time the token expires, the user must log in again manually to get a new token.
This manual re-login is annoying for users and makes your app feel broken. Also, constantly asking for passwords increases security risks and server load.
Refresh tokens let your app silently get new access tokens without bothering the user. This keeps users logged in smoothly and securely.
if (tokenExpired) { redirectToLogin(); }if (tokenExpired) { useRefreshTokenToGetNewAccessToken(); }It enables seamless, secure user sessions without repeated logins, improving user experience and security.
Think of how apps like Gmail keep you logged in all day without asking for your password every few minutes.
Manual token expiration forces annoying re-logins.
Refresh tokens automate getting new access tokens silently.
This improves security and user experience dramatically.
Practice
refresh token in an Express app using authentication?Solution
Step 1: Understand the role of refresh tokens
Refresh tokens allow the app to request new access tokens without user interaction.Step 2: Compare options with refresh token purpose
Only To get a new access token without asking the user to log in again correctly describes this purpose; others describe unrelated functions.Final Answer:
To get a new access token without asking the user to log in again -> Option DQuick Check:
Refresh token purpose = get new access token without login [OK]
- Confusing refresh token with access token
- Thinking refresh token stores passwords
- Assuming refresh token logs out users
Solution
Step 1: Recall Express method to set headers
Express usesres.setHeader(name, value)to set response headers.Step 2: Match correct syntax for refresh token header
res.setHeader('refresh-token', token); uses correct method and header name; others are invalid or use wrong header.Final Answer:
res.setHeader('refresh-token', token); -> Option AQuick Check:
Set header with res.setHeader(name, value) [OK]
- Using non-existent res.sendRefreshToken method
- Assigning token directly to res property
- Using wrong header like 'Authorization' for refresh token
app.post('/token', (req, res) => {
const refreshToken = req.body.token;
if (!refreshToken) return res.status(401).send('No token');
if (refreshToken !== 'validtoken') return res.status(403).send('Invalid token');
res.json({ accessToken: 'newAccessToken123' });
});Solution
Step 1: Check token presence and validity
IfrefreshTokenis missing, returns 401; if invalid, returns 403.Step 2: For valid token, send new access token JSON
When token equals 'validtoken', response sends JSON with new access token.Final Answer:
JSON response with new access token -> Option CQuick Check:
Valid token returns new access token JSON [OK]
- Confusing status codes for missing vs invalid token
- Expecting empty response instead of JSON
- Ignoring token validation logic
app.post('/refresh', (req, res) => {
const token = req.body.refreshToken;
if (!token) res.status(401).send('Missing token');
if (token !== 'secret') res.status(403).send('Forbidden');
res.json({ accessToken: 'newToken' });
});Solution
Step 1: Check response flow after status calls
Withoutreturn, code continues after sending response, causing errors.Step 2: Confirm need for return to stop execution
Addingreturnafterres.status(...).send(...)prevents multiple responses.Final Answer:
Missing return statements after res.status calls causing multiple responses -> Option AQuick Check:
Return after res.status to stop code [OK]
- Not returning after res.status sends response
- Assuming res.json is wrong here
- Thinking property name is incorrect
Solution
Step 1: Understand refresh token rotation
Rotation means issuing a new refresh token each time the old one is used and invalidating the old token.Step 2: Evaluate options for security best practice
Issue a new refresh token on each use and invalidate the old one matches rotation concept; others either reduce security or misuse storage.Final Answer:
Issue a new refresh token on each use and invalidate the old one -> Option BQuick Check:
Refresh token rotation = new token each use [OK]
- Reusing same refresh token indefinitely
- Not invalidating old refresh tokens
- Storing tokens insecurely on client side
