Struggling with JWT errors like invalid token or expired signature? Learn how to debug and fix JWT issues step-by-step with real examples.
JSON Web Tokens (JWT) are widely used for authentication in modern web applications. However, developers often face frustrating issues such as invalid tokens, expired tokens, or signature verification failures.
If you've ever seen errors like:
You're not alone.
In this guide, we will walk through how to debug JWT errors step-by-step, understand the root causes, and fix them using practical examples.
👉 You can also instantly inspect and debug your token using our tool: https://www.mydevtoolhub.com/tools/jwt-decoder
A JWT consists of three parts:
Header.Payload.Signature
Example:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
.
eyJ1c2VySWQiOiIxMjMiLCJleHAiOjE3MDAwMDAwMDB9
.
SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
Each part is Base64 encoded and can be decoded to understand the token.
abc.def
if (!token || token.split('.').length !== 3) {
throw new Error('Invalid JWT format');
}
Paste your token into: https://www.mydevtoolhub.com/tools/jwt-decoder
Check if:
exp field{
"userId": "123",
"exp": 1700000000
}
jwt.sign(payload, secret, { expiresIn: '1h' });
Always implement refresh tokens for better UX.
Ensure the same secret is used:
jwt.verify(token, process.env.JWT_SECRET);
If using multiple environments:
.env consistencynbf (Not Before){
"nbf": 1700000000
}
Follow this workflow whenever you face JWT issues:
Use: https://www.mydevtoolhub.com/tools/jwt-decoder
Analyze:
exp)nbf)exp with current timestampconst jwt = require('jsonwebtoken');
try {
const decoded = jwt.verify(token, process.env.JWT_SECRET);
console.log(decoded);
} catch (err) {
if (err.name === 'TokenExpiredError') {
console.log('Token expired');
} else if (err.name === 'JsonWebTokenError') {
console.log('Invalid token');
} else {
console.log('Other error:', err.message);
}
}
User suddenly logged out
Token expired after 15 minutes
Because of the exp field. Increase expiry or use refresh tokens.
Only if signature is verified.
Secret mismatch or token modified.
Better to use HTTP-only cookies for security.
JWT errors can be tricky, but with the right debugging approach, you can quickly identify and fix issues.
Always remember:
And use tools like: https://www.mydevtoolhub.com/tools/jwt-decoder
To simplify your debugging process.
Learn how to handle special characters, Unicode, emojis, and spaces in URL encoding with real examples and edge-case fixes.
Learn how to debug URL encoding issues in production using logs, network tools, and advanced developer techniques.
Master URL encoding with real-world examples including forms, search queries, APIs, and redirects. A practical guide for developers.