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

loginverify hashsigned JWTkeeps it
login happens once; the badge does the talking after that

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 RS256

authN / 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 you

httpOnly cookie

A cookie JavaScript cannot read. The browser attaches it automatically, so XSS cannot steal it.

Set-Cookie: HttpOnly

how it works

  1. 1
    Collect credentials

    POST /login with email and password over HTTPS. Never in a GET query string.

  2. 2
    Compare against the hash

    bcrypt.compare(password, hash). You never store, or even see, the original password.

  3. 3
    Sign a short-lived token

    jwt.sign with sub and role, expiring in about 15 minutes. Short life limits the blast radius.

  4. 4
    Send it with every request

    Ideally in an httpOnly cookie the browser attaches automatically, or an Authorization header.

  5. 5
    Verify 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

1import jwt from 'jsonwebtoken'
2const SECRET = process.env.JWT_SECRET
the seal-maker lives in env vars, never in git
3
4const token = jwt.sign(
claims get written and sealed in one step
5 { sub: user.id, role: 'admin' },
6 SECRET, { expiresIn: '15m' })
a stolen token dies in 15 minutes
7
8const claims = jwt.verify(token, SECRET)
one changed character and this throws

side by side

sessions

  • state lives on the server
  • revoke any time, instantly
  • simple inside one app
  • store = memory or Redis
VS

JWTs

  • stateless: server keeps nothing
  • scale wide across servers
  • valid until expiry, period
  • revoking needs a denylist
one JWTheaderpayloadsignature
two readable parts and one seal: the anatomy of a JWT
🪤 gotcha

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

🎤 interview

Authentication vs authorization?watch ↗

🎤 interview

Sessions or JWT?watch ↗

🎤 interview

How does “sign in with Google” work?watch ↗

📚 tutorial

How do you store passwords?watch ↗

🔧 useful

Who guards your routes?watch ↗

💎 deep

Where should the token live?watch ↗

💎 deep

Why do access tokens expire so fast?watch ↗

🧠 trivia

Why is bcrypt… slow?watch ↗