Track 07 · Lesson 7 of 18Identity and login
- 01Authentication vs authorization
- 02Browser storage compared
- 03Sessions versus tokens
- 04Inside a JWT
- 05Access and refresh tokens
- 06Where to keep tokens
- 07Choosing a design
- 08The login lifecycle
- 09The token clock
- 10Dead or just unlucky? Refresh decisions
- 11Inside a secure session cookie
- 12Five tabs, one refresh
- 13“Log in with Google”: OAuth and PKCE
- 14Two-factor login
- 15Storing passwords
- 16Logging out properly
- 17Trade-offs and common mistakes
- 18Compare the designs
Choosing a design
Lesson 7 of 18· 6 min
There’s no single correct auth design. The right one depends on how your app is built, where your API lives, what you can change, and how much a stolen session would hurt. Answer six questions and see the five common setups ranked.
Decision helper
Example answers are selected. Change them
1. How are your pages built?
2. Where does your API live?
3. Do you need SSO, social login or MFA without building it?
4. Who else calls your API?
5. How bad would a stolen session be?
6. Can you change the backend's auth code?
Best fit for these answers
BFF + sealed refresh cookie
A small server next to the frontend catches tokens at login, seals the refresh token in an HttpOnly cookie, and hands the browser only a short-lived access token kept in memory. Everyday API calls still go straight to the API. Strong protection with almost no backend changes.
- You can run server code next to the frontend, so a BFF is cheap to add.
- A separate API makes cross-site cookies awkward, which favors tokens or a same-origin BFF.
- With one client, cookie-based designs stay simple.
- Long-lived credentials should be out of JavaScript's reach.
- A BFF wraps an existing token API without changing it.
- +9
BFF + sealed refresh cookie
- +5
Full BFF (token handler)
- +2
Hosted identity provider
- -1
SPA + token in storage
- -3
Server sessions
Your setup marks the secret map, the lifecycle player and the comparison table in other lessons. A starting point for a team discussion, not a verdict.
The five setups#
- SPA + token in storage: the simplest to build and the riskiest. Fine for prototypes.
- Server sessions: the classic in Rails, Django, Laravel and friends. Simple, revocable, needs CSRF protection.
- BFF + sealed refresh cookie: a small server next to the frontend catches tokens at login, locks the refresh token in an HttpOnly cookie, and hands the browser only a short-lived access token.
- Full BFF (token handler): the browser never sees any token; every API call goes through your proxy.
- Hosted identity provider: a specialist service owns accounts, MFA and tokens through OpenID Connect.
The worked example#
The BFF with a sealed refresh cookie has the most moving parts, so the rest of this track uses it as the worked example: it touches every concept. Simpler setups skip pieces of it, and the lifecycle player tells you which pieces your setup skips. Press “Use this in this track” to mark your setup across the lessons.
Check yourself