DEV Community

Cover image for I Was Stuck on a JWT Bug for 2 Hours. This AI Prompt Found the Root Cause in Seconds.
helpdevtools
helpdevtools

Posted on

I Was Stuck on a JWT Bug for 2 Hours. This AI Prompt Found the Root Cause in Seconds.

We've all been there.

A bug with zero console errors. No obvious
cause. Just broken behavior that makes
absolutely no sense.

My JWT token was storing correctly after
login — I could see it in localStorage.
But after every single page refresh, the
user was logged out and the token was gone.

Silent failure. Nothing in the logs.
Hours wasted.

The Prompt That Fixed It

Instead of googling for the 100th time,
I used this structured AI prompt:


"You are a world-class debugger.
I have a bug I cannot solve.

What the code should do: authenticate a
user with JWT, store the token in
localStorage, and keep them logged in
after page refresh.

What it actually does: user logs in
successfully, token is stored, but after
page refresh the user is logged out and
the token disappears from localStorage.

Error: no error in console.

Code: `// Login function
async function loginUser(email, password) {
const response = await fetch('/api/auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email, password })
});
const data = await response.json();
localStorage.setItem('token', data.token);
window.location.href = '/dashboard';
}

// Check auth on page load
window.onload = function() {
const token = localStorage.getItem('token');
if (!token) {
window.location.href = '/login';
}
}
`
What I've tried: console.log before
setItem confirms token exists, but after
redirect it's gone.

Think step by step and solve it."


What Happened Next

Claude immediately identified the root cause:

Then gave a complete step by step diagnosis:

And a full working fix with comments:

The Root Cause

The culprit was a race condition between
localStorage.setItem and
window.location.href.

The redirect fires before the browser
finishes writing to localStorage. The page
tears down mid-write and the token silently
vanishes. No error. No warning.

I never would have thought of that.

Why This Prompt Works So Well

Most developers ask AI vague questions like
"why is my localStorage not working?" and
get generic answers.

This prompt works because it forces you to
give the AI everything it needs:

  • What the code SHOULD do
  • What it ACTUALLY does
  • The exact error
  • The exact code
  • What you already tried

The more context you give, the more
surgical the diagnosis.

Want More Prompts Like This?

I built a pack of 100 structured AI prompts
specifically for developers — organized into
8 categories:

  • Debugging (15 prompts)
  • Code Generation (15 prompts)
  • Security & Code Review (10 prompts)
  • Testing (10 prompts)
  • DevOps & Deployment (10 prompts)
  • Documentation (10 prompts)
  • Career & Interviews (10 prompts)
  • Productivity (20 prompts)

Each prompt includes:
✔ The exact prompt structure
✔ When to use it
✔ Difficulty level (Beginner/Intermediate/Advanced)
✔ Comes as PDF + searchable Notion template

👉 Get the full pack here: https://payhip.com/b/i7IZa

Drop any questions in the comments — happy
to share more examples.

Top comments (0)