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:
- The app sends the browser to
/services/oauth2/authorize?response_type=token&client_id=<KEY>&redirect_uri=<URI>&scope=api&state=<random>. - The user logs in and approves the scopes.
- Salesforce redirects to the callback with
#access_token=...&instance_url=...&id=...&state=.... - JavaScript reads the fragment, validates
state, and calls the APIs withAuthorization: Bearer. - No refresh token is issued, except for a custom-scheme callback URI used by native apps with the
refresh_tokenscope (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.