Implementing OAuth 2.0 (Part 4): Choose the Right Architecture for Your App
A practical guide to OAuth architecture for BFFs, browser SPAs, server-rendered apps, native/mobile apps, device flows and machine-to-machine services.

The most expensive OAuth mistakes usually happen before anybody writes the token-exchange code.
A team decides “we use OAuth”, picks a library, copies an authorization-code example and only later asks the question that should have come first:
What kind of client are we actually building?
A server-rendered web application, a browser-only SPA, an iPhone app, a smart TV and a background worker do not have the same security properties. They cannot protect credentials in the same way, they do not receive redirects in the same way, and they should not all be squeezed into the same flow because one tutorial happened to use it.
In August 2026, the IETF published RFC 10017 specifically for browser-based OAuth applications. It formalises an important point that experienced teams had already learned the hard way: architecture determines how much token exposure you accept.
This guide walks through the main application shapes and the OAuth model that fits each one.
First classify the client: confidential or public
OAuth distinguishes clients partly by whether they can keep authentication credentials confidential.
A backend service you operate can usually protect a client secret or private key from end users. That is a confidential client.
JavaScript delivered to a browser cannot keep a client secret. Neither can a mobile application distributed through an app store. A motivated user can inspect the code or application package. Those are public clients.
This distinction is easy to misunderstand.
Adding this to a SPA does not make it confidential:
const clientSecret = 'super-secret-production-value';
Minifying it does not help. Obfuscating it does not help. Moving it into an environment variable at build time does not help if the value ultimately ships to the browser.
Likewise, embedding a secret in an iOS or Android binary does not turn the device into a secure backend.
Client authentication only means something when the credential remains outside the attacker’s practical reach.
Architecture 1: server-rendered web application
This is still one of the cleanest OAuth models.
The browser interacts with your server. Your server performs the OAuth code exchange and keeps access and refresh tokens server-side.
Browser
│
│ application session cookie
▼
Your web server
│
│ OAuth tokens kept server-side
▼
Protected APIs
The server can be a confidential OAuth client because it can protect a secret or private key.
The browser receives an ordinary application session, typically using a cookie configured appropriately with Secure, HttpOnly and SameSite attributes.
Advantages include:
- OAuth tokens do not need to be exposed to browser JavaScript;
- refresh tokens can remain entirely server-side;
- client authentication is meaningful;
- session revocation is under your control;
- the application can centralise token renewal and API access.
The trade-off is that you operate stateful or semi-stateful backend infrastructure and must secure the browser session properly.
For many traditional SaaS products, admin portals and business applications, that is a good trade.
Architecture 2: Backend for Frontend (BFF)
The Backend for Frontend pattern has become particularly important for modern JavaScript applications.
The UI can still be a rich SPA built with Vue, React, Angular, Svelte or similar. The difference is that OAuth responsibilities live in a backend component associated with that frontend.
Current IETF browser-app guidance describes a BFF as the strongest of the three main browser OAuth architecture patterns.
The BFF:
- acts as the confidential OAuth client;
- stores the OAuth tokens server-side;
- represents the browser with a cookie-based session;
- proxies browser API requests to resource servers and attaches the correct access token.
Browser SPA
│
│ Secure HttpOnly session cookie
▼
BFF
│
├── access token
├── refresh token
│
▼
Resource API
The browser never needs to see either OAuth token.
That is an important security property. If malicious JavaScript executes in the page, it may still act through the compromised application while it is running — no web architecture magically makes XSS harmless — but the script cannot simply read a long-lived refresh token and carry it away for later use.
RFC 10017 strongly recommends the BFF pattern for business applications, sensitive applications and applications handling personal data.
A BFF is not merely an API gateway
It is tempting to describe any reverse proxy as a BFF, but the distinction matters.
A BFF is specifically part of the frontend’s security architecture. It understands the user’s application session and performs OAuth responsibilities on the browser’s behalf.
Typical BFF endpoints might include:
/login
/oauth/callback
/logout
/session
/api/* -> proxied to downstream resource APIs
When /api/orders is requested, the BFF can resolve the user’s server-side session, obtain the appropriate access token and forward the request to the actual orders API.
That means the BFF becomes security-sensitive infrastructure. It needs normal CSRF protections, strict session handling, constrained proxy behaviour, request validation, rate limiting and careful logging.
Do not build an unrestricted endpoint that allows a browser to tell the BFF which arbitrary URL to proxy with the user’s access token.
The BFF trade-off: security for additional infrastructure
A BFF is not free.
Every API call may pass through another network hop. The BFF needs capacity, observability and failure handling. If resource responses are large, proxying them increases traffic through your infrastructure. Rate limiting may need to distinguish end users even though downstream APIs see requests coming from the BFF tier.
And because the BFF observes requests and responses, there are privacy implications if that component is operated by a third party.
Those are real costs.
But compare them with what you gain: browser JavaScript no longer needs possession of OAuth access or refresh tokens.
For high-value applications, that is often worth it.
Architecture 3: token-mediating backend
RFC 10017 also defines an intermediate model.
A backend handles the OAuth authorization flow and keeps the refresh token, but it hands access tokens to the browser so the SPA can call resource servers directly.
Browser
│ ┌────────────► API
│ access token │
▼ │
Token-mediating backend
│
└── keeps refresh token
This reduces some long-term credential exposure because the refresh token stays on the backend. It also avoids proxying every API request.
But the access token is still exposed to browser code. If malicious JavaScript steals it, the attacker can call the resource server directly until the token ceases to be valid.
For that reason, current guidance places this architecture below the full BFF in security terms.
Use it because a concrete system constraint makes the BFF impractical, not merely because returning the access token is easier.
Architecture 4: browser-only OAuth client
A pure SPA can perform OAuth directly in the browser as a public client.
The flow should use Authorization Code + PKCE. There is no legitimate client secret to protect.
Browser SPA
│
├── authorization request + PKCE challenge
▼
Authorization server
│
└── code
│
▼
Browser SPA
│
├── code + PKCE verifier
▼
Token endpoint
This architecture remains valid, but it accepts more exposure.
The browser obtains OAuth tokens, so malicious code executing in the application’s origin can potentially access or use those credentials.
That makes browser security controls unusually important:
- strong Content Security Policy;
- avoidance of dangerous DOM injection;
- Trusted Types where appropriate;
- disciplined third-party script management;
- short-lived access tokens;
- secure refresh-token handling where refresh tokens are issued;
- refresh-token rotation or sender constraining as required for public clients;
- no tokens in URLs;
- no token logging.
If your application handles payroll, health information, financial administration or high-value business data, “our SPA can technically hold the tokens itself” is not the same as “that is the best architecture”.
Why the Implicit flow should not be your SPA default
Older SPA tutorials often use the OAuth Implicit flow because it returned an access token directly from the authorization endpoint.
Modern security guidance moved away from that pattern.
Authorization Code + PKCE lets tokens be issued from the token endpoint instead of exposing access tokens in the authorization response. It provides a better foundation for replay detection and sender-constraining mechanisms.
If you find a frontend tutorial that says SPAs must use response_type=token because they cannot keep a secret, you are reading outdated guidance.
Public clients do not need a secret to use Authorization Code + PKCE.
Architecture 5: native mobile and desktop applications
Native applications have their own best-current-practice guidance in RFC 8252.
The critical principle is that authorization should occur in an external user-agent — normally the system browser or a secure browser tab — rather than an embedded web view controlled by the application.
Why?
An embedded web view can place the application in a position to inspect credentials, interfere with the authorization page or create phishing-like experiences. The system browser also preserves established authentication sessions and security context.
The typical native flow is:
Native app
│
├── generate PKCE verifier/challenge
├── open system browser
▼
Authorization server
│
└── redirect
▼
OS routes redirect back to app
│
└── app exchanges code + verifier
Native apps are public clients. Do not ship a shared client secret and pretend otherwise.
Redirect URIs for native apps deserve real design
A web server can receive a normal HTTPS callback. Native apps need the operating system to route the authorization response back to the application.
Common patterns include:
- claimed HTTPS/app links;
- private-use URI schemes;
- loopback redirect URIs for desktop applications.
The exact option depends on the platform.
PKCE is particularly important because some redirect mechanisms can be subject to interception by another application claiming the same URI pattern. An intercepted authorization code is much less useful without the verifier held by the legitimate app.
Use platform-specific secure-link mechanisms where practical and follow the redirect requirements of your identity provider rather than inventing a custom callback transport.
Architecture 6: device authorization flow for input-constrained devices
A smart TV, command-line device or appliance may not have a convenient browser and keyboard for completing a normal authorization redirect.
The OAuth Device Authorization Grant addresses this case.
The device requests a device code and receives information such as a user code and verification URI.
The user completes authorization on another device:
TV / CLI Phone / laptop
│ │
│ receives code │
│ │ open verification URL
│ displays: ABCD-EFGH │ enter/confirm code
│ ▼
│ Authorization server
│ │
└──── polls token endpoint ◄─────┘
This flow is for devices with genuine input/browser constraints.
It is not a shortcut for ordinary websites that simply do not want to implement redirects.
The device must obey the server-provided polling interval and handle states such as authorization pending, slow down, access denied and code expiry correctly.
Architecture 7: machine-to-machine service clients
Not every OAuth interaction contains a human.
A backend job may need to call another API under its own identity.
This is where the Client Credentials grant is commonly appropriate.
Worker service
│
│ authenticates as OAuth client
▼
Authorization server
│
└── service access token
│
▼
Resource API
There is no user authorization redirect because the client is acting on its own behalf.
This is suitable for cases such as:
- scheduled background jobs;
- internal service-to-service API calls;
- server automation;
- data synchronisation between trusted systems.
It is not a way for a SPA to exchange a client secret for a token. A secret embedded in the browser is not confidential.
Prefer stronger client authentication where the risk warrants it
Confidential clients can authenticate to the authorization server in several ways depending on provider support.
A static client secret is common, but it creates rotation and leakage concerns.
Higher-assurance systems may use mechanisms such as:
- private-key JWT client authentication;
- mutual TLS;
- workload identity/federation patterns provided by cloud infrastructure.
The right answer depends on the environment, but the principle is useful: avoid creating permanent shared secrets when your platform supports short-lived or asymmetric identity.
The same lesson applies beyond OAuth.
Do not use the Resource Owner Password Credentials grant for new systems
The old password grant allowed the client to collect the user’s username and password and exchange them for tokens.
That undermines one of OAuth’s most valuable properties: the client should not need possession of the user’s authorization-server credentials.
Modern OAuth security guidance says the resource owner password credentials grant must not be used.
If a migration project still contains it, treat replacement as security debt. Do not copy it into new architecture because “it is simpler than redirects”.
Choose architecture based on where you can keep credentials
A useful decision table looks like this:
| Application | Typical OAuth shape | Can protect client credential? | Token location |
|---|---|---|---|
| Server-rendered web app | Authorization Code | Yes | Server |
| Browser app + BFF | Authorization Code + PKCE as appropriate | BFF: yes | BFF/server |
| Browser app + token-mediating backend | Authorization Code + PKCE | Backend: yes | Access token in browser; refresh token backend |
| Browser-only SPA | Authorization Code + PKCE | No | Browser |
| Native/mobile app | Authorization Code + PKCE | No | Device secure storage |
| Smart TV / constrained device | Device Authorization Grant | Usually public-client model | Device |
| Backend worker/service | Client Credentials | Yes | Server/workload |
The table is deliberately not “React = flow A, Vue = flow B”.
Framework choice is not the security boundary. Runtime architecture is.
OAuth security depends on the rest of the web stack too
If your login architecture depends on a browser application, audit HTTPS, CSP, framing and other exposed security controls alongside the OAuth flow.
Architecture mistakes that look reasonable at first
Putting a client secret in frontend environment variables
Frontend build-time variables often end up in the JavaScript bundle. A secret that ships to users is not a secret.
Using Client Credentials for user-facing SPA login
Client Credentials represents the client itself, not an end user. It does not become a user login flow because you attach an email address afterward.
Giving every application the same OAuth client registration
Separate applications often need separate redirect URIs, credential rotation, permissions, ownership and telemetry. One giant shared client makes incident response harder and creates unnecessary blast radius.
Letting mobile apps authenticate in embedded web views
Use the external browser/system authentication session unless a very specific platform/provider requirement says otherwise.
Building a BFF but returning refresh tokens to JavaScript
That defeats one of the principal reasons for having the BFF.
Using a device flow because redirects feel difficult
Device authorization is designed for devices with constrained input, not as a general replacement for browser redirects.
Draw the trust boundaries before implementation
Before selecting an OAuth library, draw something as simple as this:
[ Browser / device ]
│
│ trust boundary
▼
[ Our backend ]
│
│ external identity boundary
▼
[ Authorization server ]
│
▼
[ Resource APIs ]
Then annotate:
- where access tokens exist;
- where refresh tokens exist;
- where client credentials exist;
- which component performs the code exchange;
- which component receives redirects;
- which network calls cross trust boundaries;
- which component can revoke a session;
- which component is exposed to arbitrary user-controlled code.
Most bad OAuth architecture becomes obvious once credentials are drawn onto the diagram.
A practical selection process
When starting a new OAuth integration, answer these questions in order:
- Is there a human user involved?
- Is the client running on infrastructure we control or on a user’s device/browser?
- Can the client genuinely protect credentials?
- Does browser JavaScript actually need direct access to resource APIs?
- Would a BFF materially reduce token exposure?
- Does the device have a usable external browser?
- Are there regulatory or high-assurance requirements that justify sender-constrained tokens or stronger client authentication?
- How will sessions and refresh tokens be revoked?
Only after that should you choose the exact library and provider configuration.
The architecture is part of the security control
OAuth is often presented as a sequence of protocol messages. In production, the placement of those messages matters just as much.
A BFF and a browser-only SPA can both use Authorization Code + PKCE and still have very different exposure to token theft. A native app and server web app can both receive an authorization code while having completely different client-authentication assumptions. A background worker may need OAuth without any user login at all.
There is no single “best OAuth flow” detached from the application.
There is a best fit for a particular trust model.
In Part 5, we finish the series by hardening the production deployment: redirect URI validation, CSRF/state handling, mix-up attacks, key rotation, discovery metadata, secret management, logging, monitoring, incident response and the subtle configuration errors that survive code review.
