Understanding JWT Expiration

Every JWT should include an exp (expiration) claim. When the current time exceeds this value, the token must be rejected.

Fix: Implement Refresh Tokens

async function refreshAccessToken(refreshToken) {
  const res = await fetch('/auth/refresh', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ refresh_token: refreshToken })
  });
  return res.json();
}

Client-Side Detection

function isTokenExpired(token) {
  const payload = JSON.parse(atob(token.split('.')[1]));
  return payload.exp * 1000 < Date.now();
}

Best Practices

  • Keep access tokens short-lived (5–15 minutes)
  • Use refresh token rotation
  • Handle 401 responses with automatic refresh

Understanding JWT Expired Token — How to Fix in Production

Developers search for JWT Expired Token — How to Fix when building API authentication with JSON Web Tokens. JWTs are used by OAuth 2.0, OpenID Connect, Auth0, Firebase, AWS Cognito, and Keycloak. Always validate exp, iss, and aud server-side — decoding alone proves nothing about authenticity.

JWT Structure Recap

Every JWT has three dot-separated segments: header (algorithm), payload (claims), signature (proof). Use JWT Decoder to inspect and JWT Validator to verify before trusting any claim value in production code.

Common Pitfalls

  • Algorithm confusion (none attack) — whitelist allowed algorithms
  • Secrets in the payload — payload is only Base64-encoded, not encrypted
  • Ignoring clock skew on exp and nbf
  • Weak HMAC secrets — use 256-bit random keys
  • Skipping signature verification — always call verify(), not decode()
  • Storing tokens in localStorage — XSS can steal them

Further Reading

Browse related resources: JWT Decoder, JWT Validator, JWT Basics, JWT Authentication, JWT Errors, Algorithms, Glossary, and Learning Path.

Try It Now

FAQ

Why does my JWT expire?

The exp claim sets a Unix timestamp after which the token is invalid. This limits exposure if a token is stolen.

How do I refresh an expired JWT?

Use a refresh token to obtain a new access token from your auth server. Never extend exp client-side.