BFF Pattern Explained: Why API Tokens Belong on the Server, Not the Browser
Placing API access tokens directly in browser-side JavaScript exposes them to any third-party script, XSS vulnerabilities, and anyone inspecting network traffic via developer tools. The Backend-for-Frontend (BFF) pattern addresses this by routing all browser requests through a Next.js server layer, which communicates with the API privately and keeps tokens out of the client entirely. With Next.js App Router, Server Actions and Route Handlers run server-side, allowing session cookies to remain HttpOnly and eliminating cross-origin CORS complexity. The approach also removes the API's URL and authentication structure from public view, reducing the attack surface available to potential bad actors. While the pattern is well-established, implementing it consistently requires handling cookie forwarding, content-type logic, and error management — boilerplate that most projects end up rewriting from scratch.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in