Video

JWT vs Sessions in 30 Seconds

Sessions or JWTs? Both log users in. The difference is where the state lives.

SESSIONS
The server keeps the authentication state. The browser holds only an opaque
session ID in a cookie — a lookup key, not the data. Change the session
server-side and it takes effect on the very next request.

session[:user_id] = user.id

JWTs
The server signs a token and hands it to the client. The client presents it
on later requests:

Authorization: Bearer token

THE THING MOST PEOPLE GET WRONG
A JWT is signed, not encrypted. Those three base64 segments are not
protection — anyone holding the token can decode the payload and read every
claim in it. Paste one into a decoder and it's all there in plain JSON.

The signature prevents *tampering*. It hides nothing.

So: no passwords, no API keys, no personal data you wouldn't put in a URL.
Treat the payload as public.

DECODING IS NOT VERIFYING
A token that decodes cleanly is just well-formed base64. Before you trust it,
check:

• signature — against the key you actually control
• algorithm — pin it; don't let the token choose (`alg: none` is a classic)
• issuer & audience — is this token even meant for you?
• expiry — and keep the window short

AND IF YOU USE COOKIES
`HttpOnly`, `Secure`, and a sensible `SameSite` policy. Cookies authenticate
automatically, so keep CSRF protection on.

THE HONEST COMPARISON
JWTs aren't faster, safer, or truly stateless in most real systems — the
moment you need logout-everywhere or instant revocation, you're maintaining
server-side state again (a denylist, short expiry plus refresh tokens,
something). Sessions handle revocation for free because the state was
already on the server.

Sessions suit server-rendered apps. JWTs suit token-based APIs and
cross-service auth. Storage, expiry and revocation matter far more than
which one you picked.

—
#Astmation — #software #systems, #automation, and #AI-powered #applications .
Smart Technology. Secure Solutions.
https://astmation.com/

#Shorts #JWT #Authentication #WebSecurity #SessionAuth #RubyOnRails #API #BackendDevelopment #SecureCoding #SoftwareEngineering


Watch on YouTube

Back to videos