All jobs / Frontend / web developer / Security
Frontend / web developer interview question · 3–5 years
Auth token in localStorage versus an httpOnly cookie for a SPA — how do you argue it?
The strongest answer
Prefer httpOnly + Secure + SameSite cookies so XSS cannot steal the session; add CSRF protections for cookie auth
Why it works
The 2026 default in this room: cookies you cannot read from JS, plus CSRF on mutations. Name XSS as the reason.
What a good answer covers
- Assume XSS will be tried.
- Session in httpOnly Secure SameSite cookies.
- CSRF token or custom header on writes.
- Don't put bearer tokens in localStorage by default.
Answers that fall short
- Close, but not the strongest: localStorage plus a short expiry, and stop there
Short expiry reduces window, not stealability. - Weak: sessionStorage is safe enough
sessionStorage is still JS-readable and dies per tab — not a security control. - Red flag: Keep the token on a JS global and log it
Globals and logs are how tokens leave the building.
Who gets asked this
More Security questions
- You need a small inline boot script but CSP forbids unsafe-inline. What do you do?
- Two 401s trigger two refresh calls and one of them logs the user out. How do you design the client?
- Product wants full click tapes plus form field values in the frontend logger. How do you respond?
- Legal says marketing pixels cannot run before consent. How do you architect the frontend?
- A junior's PR works but has an XSS hole and no tests. How do you mentor in review?
- How do you keep consent state consistent across web, a WebView, and a pixel manager?
- Marketing wants to render a CMS HTML snippet in React. Strongest answer?
- A cookie-session SPA posts state-changing requests. What do you do in the browser about CSRF?