Refresh Tokens in Node.js Authentication
In modern authentication systems, access tokens are usually given a short lifetime for security reasons. This helps reduce risk if a token is stolen, but it also creates a challenge: users would have to log in again very frequently.
Refresh tokens solve this problem. They allow the server to issue a new access token without forcing the user to log in again every time the old access token expires.
What is a Refresh Token?
A refresh token is a special token issued during login along with an access token. The access token is used to access protected APIs, while the refresh token is used only to request a new access token when the old one expires.
This creates a more secure system because access tokens can remain short-lived, while the refresh token helps maintain the user session.
Why Refresh Tokens Are Important
- Better security: Access tokens can expire quickly
- Improved user experience: Users do not need to log in repeatedly
- Session continuity: Long-lived sessions can be maintained safely
- Scalable authentication: Commonly used in APIs, mobile apps, and modern web apps
Access Token vs Refresh Token
| Feature | Access Token | Refresh Token |
|---|---|---|
| Purpose | Access protected APIs | Generate new access token |
| Lifetime | Short | Long |
| Usage Frequency | Frequent | Occasional |
| Storage | Client side | Usually secure storage or HTTP-only cookie |
How Refresh Tokens Work
- User logs in with email and password
- Server verifies credentials
- Server generates both access token and refresh token
- Client uses access token to call protected APIs
- When access token expires, client sends refresh token to the server
- Server verifies refresh token and issues a new access token
Generating Access and Refresh Tokens
In Node.js, refresh tokens are often implemented using the jsonwebtoken package.
In this example:
- The access token expires in 15 minutes
- The refresh token expires in 7 days
- Different secret keys are used for better security separation
Login API Example
During login, both tokens can be generated and returned:
Refreshing the Access Token
When the access token expires, the client can send the refresh token to a dedicated endpoint.
Why Access Tokens Should Be Short-Lived
If an attacker gets access to a valid access token, they may use it until it expires. Keeping access tokens short-lived reduces the time window for abuse.
Refresh tokens help balance security and usability by renewing access tokens only when needed.
Where to Store Refresh Tokens
Refresh tokens should be stored more securely than access tokens because they have a longer lifetime.
- HTTP-only cookies: Common choice for web applications
- Secure device storage: Common for mobile apps
- Database-backed token store: Useful for revocation and tracking
In many production systems, refresh tokens are stored in the database so they can be revoked if needed.
Token Rotation
A stronger security approach is refresh token rotation. In this method, each time a refresh token is used, the server generates a new refresh token and invalidates the old one.
This reduces the risk of replay attacks and makes stolen refresh tokens less useful.
Logout with Refresh Tokens
Logout should not only remove the token from the client side, but also invalidate the refresh token on the server if you are storing it in the database.
Best Practices for Refresh Tokens
- Use short expiry for access tokens
- Use longer expiry for refresh tokens
- Use different secrets for access and refresh tokens
- Store refresh tokens securely
- Consider refresh token rotation
- Revoke refresh tokens on logout or suspicious activity
- Use HTTPS in production
Common Mistakes
- Using the same secret for all token types
- Giving refresh tokens extremely long lifetimes without revocation
- Storing refresh tokens insecurely in frontend code
- Not validating refresh tokens properly
- Not revoking refresh tokens after logout
Real-World Use Cases
- Single-page applications with login sessions
- Mobile app authentication
- Secure API access with long-lived sessions
- Enterprise authentication systems
Refresh Tokens vs Re-Login
Without refresh tokens, users must log in again whenever the access token expires. With refresh tokens, the session continues smoothly in the background as long as the refresh token remains valid.
This is why refresh tokens are widely used in modern authentication systems.
Conclusion
Refresh tokens are an important part of secure authentication design. They allow access tokens to stay short-lived while still giving users a smooth login experience.
When implemented correctly with secure storage, token rotation, and proper validation, refresh tokens make JWT-based authentication much more practical for real-world applications.

