Auth & JWT
Passwords travel the internet. Let’s disguise them.
▶ the lesson · narrated sketch video · tap for sound
the intro
Authentication proves who you are; authorization decides what you may do. The common flow: user logs in, server issues a signed token (like a JWT), and the client sends it with every request. Sessions keep state on the server; JWTs carry proof with them.
the sample
the token handshake
POST /login { email, password }
→ { token: "eyJhbGci..." }
GET /me
Authorization: Bearer eyJhbGci...fun fact ✦
bcrypt is deliberately slow — a 100ms password check ruins brute-force attacks.
the docs
the deep dive
the ideas behind the video — concepts, diagrams, a walkthrough and the traps to avoid
A JWT is a laminated ID badge. Anyone in the room can read what it says, that is the point, but the signature strip at the bottom can only be produced by whoever issued it. Alter one letter and the seal breaks on the next check. Tokens live on the client because they are verified, not because they are secret.
signed, not secret
key terms
claim
A fact packed inside the token: user id, role, expiry. Readable by anyone, changeable by no one.
{ "role": "admin" }signature
The tamper seal, made with the server's secret. If anything in the token changed, verification fails.
HS256 or RS256authN / authZ
Authentication asks who you are; authorization asks what you may do. Login answers the first, roles answer the second.
who are you vs may youhttpOnly cookie
A cookie JavaScript cannot read. The browser attaches it automatically, so XSS cannot steal it.
Set-Cookie: HttpOnlyhow it works
- 1Collect credentials
POST /login with email and password over HTTPS. Never in a GET query string.
- 2Compare against the hash
bcrypt.compare(password, hash). You never store, or even see, the original password.
- 3Sign a short-lived token
jwt.sign with sub and role, expiring in about 15 minutes. Short life limits the blast radius.
- 4Send it with every request
Ideally in an httpOnly cookie the browser attaches automatically, or an Authorization header.
- 5Verify at the door
Auth middleware checks the signature and expiry before any protected handler runs.
the code, explained
mint it, then trust nothing until verified
side by side
sessions
- state lives on the server
- revoke any time, instantly
- simple inside one app
- store = memory or Redis
JWTs
- stateless: server keeps nothing
- scale wide across servers
- valid until expiry, period
- revoking needs a denylist
There is no logout button for a JWT. Once signed and delivered it is valid until it expires; deleting it from the browser changes nothing, and a stolen copy works from anywhere. That is exactly why access tokens live minutes, not days.
quick answers
Is the JWT payload encrypted?
No. It is base64; paste one into a decoder and read it. The signature stops tampering, not reading. Never put secrets in claims.
localStorage or cookies for the token?
httpOnly cookie. localStorage is readable by any script on the page, so one XSS hole drains every token in it.
Then why do refresh tokens exist?
They let access tokens stay short. A long-lived refresh token quietly mints new access tokens, and it can be stored and revoked server-side.
quick hits about Auth & JWT
short answers · interviews, tips, trivia & hidden gems
Authentication vs authorization?watch ↗
Sessions or JWT?watch ↗
How does “sign in with Google” work?watch ↗
How do you store passwords?watch ↗
Who guards your routes?watch ↗
Where should the token live?watch ↗
Why do access tokens expire so fast?watch ↗
Why is bcrypt… slow?watch ↗