Imagine you have a list of usernames and passwords. Then a user logs in to your web application. You check and verify that they are indeed on the list. So you let them in. Case solved, yes?
Not quite.
Logging in is only the beginning. What happens when they try editing their profile, posting a comment, or retrieving data they own? Of course, they should have access to it because it's theirs. But we can't blindly trust every request just because we verified them once.
You know it's them, but how can you prove it?
Well, what if we could pop the login form every single time they do something?
NO.
That would be a nightmare for the user experience. Just imagine this scenario:
Frustrating, isn't it? Nobody wants to prove their identity every time they do something. It's like having to solve a CAPTCHA over and over just to prove you're not a bot.
Thankfully, there's a better way. This is where JWT comes in.
What The Heck is a JWT?
This is literally my reaction when I first read about the term when building my Kanban backend. But it's actually quite simple.
A JSON Web Token (JWT) is a signed token that a server can issue to a client after successfully authenticating a user. I know it's a lot of terms but we can think of it this way. It's like a key is handed to you after logging in.
Then you can use this key to do all sorts of actions in the application instead of constantly being asked for credentials. The key is the proof needed by the app to know it is still you. Think of it as opening locked doors when doing an action.
Now that you have the basics of JWT, let's get more technical.
What Does It Actually Look Like?
Well, it's a string. Literally just a string. It is divided into three parts: the header, the payload, and the signature. See the example below:
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJub29ibWFzdGVyNjkiLCJyb2xlIjoidXNlciIsIm1lc3NhZ2UiOiJUaG9yJ3Mgc2VjcmV0IGVuZW15IiwicGFzc3dvcmQiOiJJIGxvdmUgeW91IDMwMDAiLCJleHAiOjE3OTU1OTkxODB9.kPlUDMKpcAw9izsnV3VBkF3kyInMwe4oPmdoqSas2vU'
If you look closely, the three parts are separated by two dots. In this example, we are using HS256 as the signing algorithm:
- Header: Contains information about the token, such as the signing algorithm used.
- Payload: The data the token carries such as the user's ID.
- Signature: The part that helps verify whether the token has been tampered with.
You may ask, how could someone tamper with it? It looks like it's already encrypted. This is also what I thought but it turns out it's not.
The payload is encoded, not encrypted.
Encoding simply transforms data into a different format so it's easier to transport, while encrypting transforms data so that only someone with the key can decrypt and read it. Anyone who gets their hands on a JWT can decode it back into a readable format.
So if JWT is encoded, couldn't an attacker modify its payload? How can the backend trust it now?
Remember the third part of JWT? The signature helps the backend verify whether the token has been tampered with. The backend recreates the signature using the same signing algorithm and signing key. If the recreated signature doesn't match, the token is rejected.
But what if the attacker changes the signature too?
They can't without the signing key. They may know which algorithm is being used, but they don't have the environment key needed to create a valid signature (yet another reason why we don't commit our .env secrets to Git!)
So, How Does the Backend Use JWT?
It starts when a user logs in. After a successful login, the backend issues a JWT. Whenever the user does something in the app, the JWT is attached to the request. The backend verifies the signature and relevant claims (i.e. expiration date). If everything checks out, the request proceeds to the next step.
In this example, JWT solves authentication across multiple requests without having to ask the user to log in every single time an action is made.
But remember, this only solves authentication. It doesn't mean the user is allowed to do everything. There should be limitations and that's where the last section comes in.
A Few Things To Note
Authentication is not the same as authorization. JWT helps establish a user's identity, which is part of authentication. Authorization, on the other hand, determines whether that user has permission to perform a particular action or access a resource.
Next, tokens shouldn't last forever. In fact, a short-lived JWT is preferred. Expiration time is a must so that stolen tokens don't remain valid indefinitely. That's where refresh tokens come in (you can look them up if you want to learn more).
Lastly, don't put sensitive information like passwords in the JWT payload. Remember, it is only encoded, not encrypted. Utilize security techniques such as using HTTPS only and proper token storage.
That's pretty much it. I was actually planning on adding more but I feel like that would be too technical. Hopefully, this article shed more light about how JWT works. Breaking it down certainly helped me and I hope it did the same to you.
Just remember, whenever you log in, just know that web apps are always checking your identity on every single action you do. They are checking. Always checking.
PS. I drew some of the illustrations here by recreating drawings I found online and others from libraries I downloaded. I think this is the closest I could get to making a blog post feel like a YouTube explainer video, so I hope that goal was achieved.
By the way, try decoding the JWT string example above. If you do, then congrats on discovering a Marvel easter egg! That'll be your reward for making it this far. Have a good one!







Top comments (15)
Great detail article on JWT Elmar!
Thank you @francistrdev!
The key analogy works well. One thing I'd want in the follow-up next to logout is where the browser keeps that key. In localStorage any script on the page can read it, so a single XSS hands over the key to every door. An httpOnly cookie keeps it out of JavaScript's reach, at the cost of having to think about CSRF. For a Kanban backend it's a small decision now and an annoying migration later.
Right on. Been reading a lot about XSS and CSRF so everything was kind of new to me. Thanks for explaining this a bit further.
Decoded the example token — 'Thor's secret enemy' and 'I love you 3000' sitting in plain base64 is the whole lesson in one string. Anyone can read it; the signature's the only thing stopping them rewriting it. The short-lived + refresh token bit is the part I'd want the follow-up on — do you rotate the refresh token on every use, or is that a whole second post?
Ohh, thanks for cracking the code. Yep, I was tempted to add the refresh token topic here but I feel like it would be too much for a single post. For the next one? I'm thinking of a completely different topic!
Clear walkthrough of the encoding vs. signature distinction, which trips up a lot of first-time backend builds. When wiring up jwt.verify() in Node/Express, one subtle edge case worth pinning down is passing an explicit algorithms: ['HS256'] allowlist rather than trusting the token header's alg field, so alg: none or key-confusion payloads get rejected immediately. Storing those short-lived tokens in HttpOnly; SameSite=Lax cookies instead of localStorage also prevents stray client-side scripts from exfiltrating them.
Yes, I believe I did SameSite=Lax and HttpOnly on my first backend. But explicitly saying I only allow 'HS256' is new to me. It make sense actually, if not the same, the token is rejected ASAP. Thanks for the in-depth explanation!
Great framing. Knowing it's the same person and being able to prove it are different problems, and most auth bugs live in the gap between them. Nice walk through it.
fun next one to write: logout. the token stays valid until exp no matter what the user clicks, and that's usually where people first meet short expiry plus a denylist
Ohhh, I'll keep this in mind!
The noobmaster69 payload in the example token was a fun touch, and keeping the distinction between base64 encoding and actual encryption clear is something a lot of JWT intros skip
Glad that distinction came out well. Thanks @parimalmakes
I think the encoding versus encryption distinction was the clearest part for me.
Thanks! I did quite a lot of thinking of how I could translate it in real world visuals.