DevNexus LogoDevNexus
ToolsBlogAbout
K
Browse Tools
HomeBlogDebug JWT Errors Fix Invalid Expired Signature
DevNexus LogoDevNexus

A free, open-source toolkit of developer utilities. Built by developers, for developers.

Tools

  • All Tools
  • Text Utilities
  • Encoders
  • Formatters

Resources

  • Blog
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

© 2026 MyDevToolHub

Built with Next.js 16 + MongoDB · Crafted for developers

jwtjwt debuggingauthenticationnodejsweb security

How to Debug JWT Errors: Fix Invalid, Expired & Signature Issues (Complete Guide)

Struggling with JWT errors like invalid token or expired signature? Learn how to debug and fix JWT issues step-by-step with real examples.

DT
MyDevToolHub Team
Mar 18, 20265 min read

Related tools

Browse all tools
Jwt DecoderOpen jwt-decoder tool

Introduction

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:

  • "Invalid token"
  • "jwt malformed"
  • "jwt expired"
  • "signature verification failed"

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


What is a JWT (Quick Recap)

A JWT consists of three parts:

Code
Header.Payload.Signature

Example:

Code
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
.
eyJ1c2VySWQiOiIxMjMiLCJleHAiOjE3MDAwMDAwMDB9
.
SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

Each part is Base64 encoded and can be decoded to understand the token.


Common JWT Errors and How to Fix Them

1. "jwt malformed" Error

Cause:

  • Token is not in correct format
  • Missing parts (header, payload, signature)

Example Problem:

Code
abc.def

Fix:

  • Ensure token has 3 parts separated by dots
  • Validate token before using it
Code
if (!token || token.split('.').length !== 3) {
  throw new Error('Invalid JWT format');
}

2. "Invalid Token" Error

Cause:

  • Token is corrupted
  • Improper encoding

Debug Tip:

Paste your token into: https://www.mydevtoolhub.com/tools/jwt-decoder

Check if:

  • Header and payload are readable
  • JSON structure is valid

3. "jwt expired" Error

Cause:

  • Token has expired based on exp field

Example Payload:

Code
{
  "userId": "123",
  "exp": 1700000000
}

Fix:

  • Refresh token
  • Increase expiry time (if appropriate)
Code
jwt.sign(payload, secret, { expiresIn: '1h' });

Pro Tip:

Always implement refresh tokens for better UX.


4. "Invalid Signature" Error

Cause:

  • Secret key mismatch
  • Token tampered

Fix:

Ensure the same secret is used:

Code
jwt.verify(token, process.env.JWT_SECRET);

If using multiple environments:

  • Check .env consistency
  • Avoid hardcoding secrets

5. "jwt not active" Error

Cause:

  • Token used before nbf (Not Before)

Example:

Code
{
  "nbf": 1700000000
}

Fix:

  • Ensure server time is correct
  • Avoid future timestamps

Step-by-Step JWT Debugging Process

Follow this workflow whenever you face JWT issues:

Step 1: Validate Format

  • Check if token has 3 parts

Step 2: Decode Token

Use: https://www.mydevtoolhub.com/tools/jwt-decoder

Analyze:

  • Payload data
  • Expiration (exp)
  • Not before (nbf)

Step 3: Verify Signature

  • Confirm secret key
  • Match algorithm (HS256, RS256)

Step 4: Check Expiry

  • Compare exp with current timestamp

Step 5: Validate Environment

  • Same secret across backend
  • Correct timezone

Debugging JWT in Node.js (Practical Example)

Code
const 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);
  }
}

Best Practices to Avoid JWT Errors

  • Always validate token format
  • Use environment variables for secrets
  • Implement refresh tokens
  • Set proper expiration time
  • Sync server time (use NTP)

Real-World Debugging Scenario

Problem:

User suddenly logged out

Cause:

Token expired after 15 minutes

Solution:

  • Increase expiry
  • Add refresh token logic

FAQs

Q1: Why does my JWT keep expiring?

Because of the exp field. Increase expiry or use refresh tokens.

Q2: Can I trust decoded JWT data?

Only if signature is verified.

Q3: Why signature fails?

Secret mismatch or token modified.

Q4: Should I store JWT in localStorage?

Better to use HTTP-only cookies for security.


Conclusion

JWT errors can be tricky, but with the right debugging approach, you can quickly identify and fix issues.

Always remember:

  • Decode first
  • Verify second
  • Check expiry

And use tools like: https://www.mydevtoolhub.com/tools/jwt-decoder

To simplify your debugging process.

On This Page

  • Introduction
  • What is a JWT (Quick Recap)
  • Common JWT Errors and How to Fix Them
  • 1. "jwt malformed" Error
  • 2. "Invalid Token" Error
  • 3. "jwt expired" Error
  • 4. "Invalid Signature" Error
  • 5. "jwt not active" Error
  • Step-by-Step JWT Debugging Process
  • Step 1: Validate Format
  • Step 2: Decode Token
  • Step 3: Verify Signature
  • Step 4: Check Expiry
  • Step 5: Validate Environment
  • Debugging JWT in Node.js (Practical Example)
  • Best Practices to Avoid JWT Errors
  • Real-World Debugging Scenario
  • Problem:
  • Cause:
  • Solution:
  • FAQs
  • Q1: Why does my JWT keep expiring?
  • Q2: Can I trust decoded JWT data?
  • Q3: Why signature fails?
  • Q4: Should I store JWT in localStorage?
  • Conclusion

You Might Also Like

All posts

Handling Special Characters, Unicode, and Spaces in URL Encoding (Advanced Guide for Developers)

Learn how to handle special characters, Unicode, emojis, and spaces in URL encoding with real examples and edge-case fixes.

Mar 18, 20267 min read

Debugging URL Encoding Issues in Production Applications (Advanced Developer Guide)

Learn how to debug URL encoding issues in production using logs, network tools, and advanced developer techniques.

Mar 18, 20267 min read

Real-World URL Encoding Examples Every Developer Should Know (Practical Guide)

Master URL encoding with real-world examples including forms, search queries, APIs, and redirects. A practical guide for developers.

Mar 18, 20267 min read