Start now →

OAuth2 BFF for SPAs — Stop Putting Tokens in localStorage

By Jitin Kayyala · Published April 13, 2026 · 1 min read · Source: Level Up Coding
Security
OAuth2 BFF for SPAs — Stop Putting Tokens in localStorage

Member-only story

OAuth2 BFF for SPAs — Stop Putting Tokens in localStorage

Jitin KayyalaJitin Kayyala12 min read·1 hour ago

--

Press enter or click to view image in full size
OAuth2 BFF SPA

Imagine you build a React app and it stores the access token in localStorage. A simple XSS exploit—a malicious script injected through a compromised third-party library or an unvalidated user input—reads the token and sends it to an attacker's server.

Game over.

The attacker now has full access to your user's account until the token expires.

This is the problem with treating Single Page Applications (SPAs) as OAuth2 “public clients.” Browsers are hostile environments.

Tokens stored in localStorage or sessionStorage are readable by any JavaScript running on the page. Even httpOnly cookies aren't enough when the SPA itself calls the token endpoint—the authorization code and client credentials are still exposed in browser memory.

The Backend for Frontend (BFF) pattern solves this by moving OAuth2 entirely server-side.

If you need more details of the BFF pattern check this blog

Backend for Frontend (BFF) in Java — A Practical Guide

Let’s start with the problem that Backend for Frontend pattern solves :

levelup.gitconnected.com

This article was originally published on Level Up Coding and is republished here under RSS syndication for informational purposes. All rights and intellectual property remain with the original author. If you are the author and wish to have this article removed, please contact us at [email protected].

NexaPay — Accept Card Payments, Receive Crypto

No KYC · Instant Settlement · Visa, Mastercard, Apple Pay, Google Pay

Get Started →