# When should developers utilize the OAuth 2.0 User-Agent Flow for client-side application integrations?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, User-Agent Flow, Client-Side Apps

Read it on the site: https://www.forcechallenges.com/interview-questions/when-should-developers-utilize-the-oauth-2-0-user-agent-flow-for-client-side-application-integrations

## Answer

**Basics:** In this flow the client redirects the user to Salesforce to log in, and Salesforce returns the access token in the **URL fragment** (after `#`) of the redirect URI, with no back-channel code exchange. It exists because a public client (single-page app, desktop or mobile app) cannot keep a consumer secret confidential.

**How it works:**
1. The app sends the browser to `/services/oauth2/authorize?response_type=token&client_id=<KEY>&redirect_uri=<URI>&scope=api&state=<random>`.
2. The user logs in and approves the scopes.
3. Salesforce redirects to the callback with `#access_token=...&instance_url=...&id=...&state=...`.
4. JavaScript reads the fragment, validates `state`, and calls the APIs with `Authorization: Bearer`.
5. No refresh token is issued, except for a custom-scheme callback URI used by native apps with the `refresh_token` scope (verify in current docs).

**Advanced / deep dive:** The implicit pattern has weaknesses: tokens appear in browser history, referrers and logs; no client authentication; no refresh. The OAuth security best current practice and OAuth 2.1 drop implicit in favor of **Authorization Code with PKCE** (`code_challenge`, `code_verifier`), which Salesforce supports for public clients and which can also return refresh tokens. Org defaults may restrict or disable the user-agent flow, so verify whether your org allows it. If a SPA must call Salesforce, an even safer design is a backend (Backend-for-Frontend) holding tokens with the Web Server flow. Also consider the **Lightning Web Security/Experience Cloud** options when the app is hosted inside Salesforce, where no OAuth is needed because the user session already exists.

**Code / config example:**
```
GET https://MyDomain.my.salesforce.com/services/oauth2/authorize
  ?response_type=token&client_id=3MVG...&redirect_uri=https%3A%2F%2Fapp.example.com%2Fcb
  &scope=api&state=xyz123
```
Response redirect: `https://app.example.com/cb#access_token=00D...&instance_url=...&token_type=Bearer`

**Hidden gotchas & must-remember points:**
- The token comes back in the fragment, not the query string.
- No client secret is used.
- Generally no refresh token; users re-authenticate when the session expires.
- Always validate `state` to block CSRF.
- Redirect URIs must match the Connected App exactly.
- Prefer Authorization Code + PKCE for new public clients.

**Best practice:** Treat the User-Agent flow as legacy; build new browser and mobile clients on authorization code with PKCE and short-lived tokens.

---
Source: ForceChallenges · last updated 2026-11-02
