SproutStackSproutStack home

Auth Basics: JWT & OAuth

5 min readBeginner-friendlyFree to read

Before this, it helps to know: API Design Basics.

Read a little, play a little. No scary maths, and no rush.

Share:

Proving "it's really me," at scale

Every app eventually asks: who is this, and may they do that? Authentication (who) and authorization (may they) sound similar, fail differently, and both must work across many servers that share nothing. Two architectures dominate, and interviews expect you to compare them.

Sessions vs tokens

  • Sessions (stateful): server stores session_id → user (in Redis, say) and hands the browser an opaque cookie. Validation = one lookup. Revocation is trivial (delete the row) — but every request needs that store, and it must be shared/fast.
  • JWTs (stateless): the token itself carries claims (sub, exp, iss, aud), signed by the server. Any service validates offline via signature — no lookup, perfect for microservices. The price: revocation is hard (you can't unsend a signed token), so keep access tokens short-lived (minutes) and rotate via refresh tokens in httpOnly cookies.

OAuth: "log in with…" without passwords

Nobody wants your password — they want Google's verdict. The flow: your app redirects to Google → user consents ("share email?") → Google returns a one-time code → your server exchanges it for tokens directly with Google. The code-in-URL step is public; the exchange is server-to-server and secret. Never put tokens in frontend logs or URLs.

Where validation lives

Check iss (who minted it), aud (who it's for), exp (still fresh?) at the API gateway, once, before traffic fans out. Downstream services trust the gateway's verdict — one checkpoint, consistently enforced.

tokensrequesttrustedBrowserHttpOnly cookieAuth serverissues tokensAPI gatewaychecks iss, aud, expOrders serviceProfile service
The gateway checks every token before traffic fans out. Services behind it trust that check, but still verify the user owns each record they touch.

Where tokens live in a browser

Tokens kept in localStorage can be read by any JavaScript on the page, so a single cross-site scripting bug can steal them. Cookies marked HttpOnly cannot be read by scripts at all, and Secure keeps them on HTTPS. Cookies bring their own risk, cross-site request forgery, which the SameSite cookie setting largely prevents. For most web apps, HttpOnly, Secure, SameSite cookies are the safer default.

Authorization: check every object

The most common real-world access bug is not a broken login; it is a missing ownership check. A logged-in user changes /orders/41 to /orders/42 in the URL and sees someone else's order, because the server checked "is this user logged in?" but not "does this order belong to this user?" Every request that touches a specific record must verify that the user may access that record. Roles (admin, editor, viewer) decide which actions are allowed; ownership checks decide which records.

Passwords and the alternatives

If you store passwords, store only a slow, salted hash made with a purpose-built algorithm such as bcrypt or Argon2, never the password itself or a fast general hash. Offer multi-factor authentication for anything sensitive. Many apps now avoid passwords altogether with OAuth sign-in or magic links sent by email, which moves the hardest security problem to providers built to handle it.

Remember this

  • Sessions revoke easily; JWTs scale easily. Short-lived access + rotating refresh splits the difference.
  • OAuth = consent → code → server-side exchange. Tokens never in frontend logs.

Check your understanding

5 questions · first-try answers earn the most XP

1. Why is revocation the main drawback of stateless JWT authentication?
2. In the OAuth code flow, why does the authorisation code get exchanged for tokens by your server rather than the browser?
3. What should a gateway check on an incoming JWT before passing traffic downstream?
4. Why are session cookies usually marked httpOnly in production?
5. When does a stateful server-side session actually beat a stateless JWT in a real product?

My notes

Saved in this browser. Highlight a line above and save it, or write it in your own words.

Nothing saved yet. Your highlights will live here.

References

Finished reading?

Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.

Try a design drill

End of Foundations

Take the section checkpoint

A short mixed quiz on all 4 chapters, to check you are ready to move on.

Related chapters

Spotted a mistake or want a topic covered? Report an issue

📬 An occasional email when new lessons land

New lessons and showdowns, no more than a few times a month. No spam, unsubscribe anytime. We store your email only to send the newsletter.