Learn how to authenticate users for Employee Agent mode using the Salesforce Mobile SDK integration or direct tokens.
Overview
Employee Agent authentication supports two approaches:
Bridge Auth (via Mobile SDK)
The Salesforce Mobile SDK handles the OAuth login flow natively. This is the recommended approach for production apps.
1JS calls loginForEmployeeAgent()2 → EmployeeAgentAuthBridge.login()3 → SalesforceSDKManager shows OAuth login screen4 → User logs in5 → Returns AuthCredentials to JS6 → JS passes credentials to AgentforceService.configure()
Direct Token
You provide an OAuth accessToken directly in the EmployeeAgentConfig. This is useful for development, testing, or when your app manages its own auth flow.
Mobile SDK Integration
To use the Mobile SDK for authentication, configure your project for each platform.
The bridge library declares SalesforceReact as compileOnly. Your host app must provide the dependency at runtime.
AgentforcePackage uses reflection to detect if SalesforceSDKManager is on the classpath. If found, EmployeeAgentAuthBridge is registered as a native module. If not, Employee Agent auth is gracefully disabled.
Auth Functions
All auth functions are exported directly from the package:
Returns true if the build includes Mobile SDK and the EmployeeAgentAuthBridge native module is available. Use this to conditionally show/hide Employee Agent UI in your app.
1interface AuthCredentials {2 instanceUrl: string; // Salesforce instance URL3 organizationId: string; // Org ID4 userId: string; // User ID5 accessToken: string; // Current OAuth access token6 refreshToken?: string; // Refresh token (may not be available on all platforms)7}
Complete Login Flow
Here’s a complete login flow for Employee Agent with Mobile SDK:
1import {2 AgentforceService,3 isEmployeeAgentAuthSupported,4 hasEmployeeAgentSession,5 loginForEmployeeAgent,6 getEmployeeAgentCredentials,7} from "@salesforce/react-native-agentforce";89async function launchEmployeeAgent(agentId?: string) {10 // Step 1: Check if auth bridge is available11 const authSupported = await isEmployeeAgentAuthSupported();12 if (!authSupported) {13 throw new Error("Employee Agent auth is not available in this build.");14 }1516 // Step 2: Check for existing session, or login17 let creds = await getEmployeeAgentCredentials();18 if (!creds) {19 creds = await loginForEmployeeAgent();20 }2122 // Step 3: Get stored agent ID (or use provided one)23 const resolvedAgentId = agentId || (await AgentforceService.getEmployeeAgentId());2425 // Step 4: Configure Employee Agent26 await AgentforceService.configure({27 type: "employee",28 instanceUrl: creds.instanceUrl,29 organizationId: creds.organizationId,30 userId: creds.userId,31 agentId: resolvedAgentId || undefined,32 accessToken: creds.accessToken,33 });3435 // Step 5: Launch conversation36 await AgentforceService.launchConversation();37}
Token Refresh
The SDK supports both automatic and manual token refresh.
Automatic Refresh
The native SDK automatically fetches fresh tokens from the Mobile SDK when the current token expires. The UnifiedCredentialProvider on both platforms integrates with the Mobile SDK’s user account system.
Manual Refresh
For scenarios where you need explicit control:
1try {2 const newCreds = await refreshEmployeeAgentCredentials();3 console.log("New access token:", newCreds.accessToken);45 // Optionally reconfigure with new token6 await AgentforceService.configure({7 type: "employee",8 instanceUrl: newCreds.instanceUrl,9 organizationId: newCreds.organizationId,10 userId: newCreds.userId,11 accessToken: newCreds.accessToken,12 });13} catch (error) {14 // Token refresh failed -- may need to re-login15 console.error("Refresh failed:", error);16}
Direct Token Mode
If you don’t want to integrate the Mobile SDK but still need Employee Agent functionality, you can provide tokens directly:
1// Obtain token through your own auth mechanism2const token = await myAuthService.getAccessToken();34await AgentforceService.configure({5 type: "employee",6 instanceUrl: "https://myorg.my.salesforce.com",7 organizationId: "00Dxx0000001234",8 userId: "005xx0000001234",9 agentId: "0Xxxx0000001234",10 accessToken: token,11});1213await AgentforceService.launchConversation();
In this mode:
isEmployeeAgentAuthSupported() returns false.
You’re responsible for obtaining, storing, and refreshing tokens.
The native SDK won’t automatically refresh the token.
When the token expires, the conversation may fail. You’ll need to obtain a new token and call configure() again.
Local Config Override File
For development and testing, you can create a local configuration file that contains your Employee Agent settings and is not committed to source control.
The bridge package exports these constants and the validation function. If the local override file exists, its exports are used; otherwise, defaults are returned (EMPLOYEE_AGENT_ENABLED: false, empty config).
Add the file to your .gitignore:
1# Employee Agent local config (contains tokens)2**/employeeAgentConfig.local.ts
For more details on these exports, see the Authentication Reference in the React Native SDK reference documentation.
Session Management
Use these functions to check and manage the user’s authentication session.
Checking Session State
1// Is auth available in this build?2const supported = await isEmployeeAgentAuthSupported();34// Is the user currently logged in?5const loggedIn = await hasEmployeeAgentSession();67// Get full credentials (or null)8const creds = await getEmployeeAgentCredentials();
Logout
1async function handleLogout() {2 // Close any active conversation3 await AgentforceService.closeConversation();45 // Logout via Mobile SDK6 await logoutEmployeeAgent();78 // Reset SDK state9 await AgentforceService.resetSettings();10}
Session Persistence
The Mobile SDK handles session persistence natively. Tokens are stored securely and survive app restarts. The EmployeeAgentAuthBridge delegates all storage to the Mobile SDK.