---
schema_version: "1"
id: 1dea865d68c86a1a70e30abee9fe54f1fef3a85f40c6d46c8339abe7b9310594
title: Employee Agent Authentication in React Native
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/genai/agentforce-mobile-sdk/guides/react-native/agentforce-mobile-sdk-react-native-employee-auth.md
canonical_url: https://developer.salesforce.com/docs/ai/agentforce-mobile-sdk/guide/agentforce-mobile-sdk-react-native-employee-auth.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 92027972bd952cd86f960a75adef4b6ae1c86e04f24318142616ea331d622f9a
products:
  - Agentforce Mobile SDK
  - Agentforce and Einstein Generative AI
---
# Employee Agent Authentication in React Native

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.

```
JS calls loginForEmployeeAgent()
  → EmployeeAgentAuthBridge.login()
    → SalesforceSDKManager shows OAuth login screen
      → User logs in
    → Returns AuthCredentials to JS
  → 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.

### iOS Setup

Use the `WithMobileSDK` subspec in your Podfile:

```ruby
pod 'ReactNativeAgentforce/WithMobileSDK', :path => '../node_modules/@salesforce/react-native-agentforce/ios'
```

This adds a dependency on `SalesforceSDKCore`, which provides OAuth login/logout flows, user account management, and token storage and refresh.

### Android Setup

Add `SalesforceReact` as a runtime dependency in your `android/app/build.gradle`:

```gradle
dependencies {
    implementation "com.salesforce.mobilesdk:SalesforceReact:13.1.1"
}
```

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:

```tsx
import {
  isEmployeeAgentAuthSupported,
  isEmployeeAgentAuthReady,
  hasEmployeeAgentSession,
  loginForEmployeeAgent,
  logoutEmployeeAgent,
  getEmployeeAgentCredentials,
  refreshEmployeeAgentCredentials,
} from "@salesforce/react-native-agentforce";
```

### isEmployeeAgentAuthSupported()

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.

```tsx
const supported = await isEmployeeAgentAuthSupported();
```

### hasEmployeeAgentSession()

Returns `true` if the user is currently logged in with valid credentials.

```tsx
const loggedIn = await hasEmployeeAgentSession();
```

### loginForEmployeeAgent()

Launches the Mobile SDK's OAuth login flow. Returns `AuthCredentials` on success.

```tsx
try {
  const creds = await loginForEmployeeAgent();
  console.log("Logged in:", creds.userId);
} catch (error) {
  // User cancelled, auth bridge unavailable, or login failed
  console.error("Login failed:", error);
}
```

### logoutEmployeeAgent()

Logs out the current user via Mobile SDK. Clears stored credentials and tokens.

```tsx
await logoutEmployeeAgent();
```

### getEmployeeAgentCredentials()

Returns the current auth credentials if the user is logged in, or `null` if no session exists.

```tsx
const creds = await getEmployeeAgentCredentials();
if (creds) {
  console.log(`Logged in as ${creds.userId}`);
}
```

### refreshEmployeeAgentCredentials()

Asks the Mobile SDK to refresh the current session and returns new credentials.

```tsx
const newCreds = await refreshEmployeeAgentCredentials();
```

### AuthCredentials Interface

```tsx
interface AuthCredentials {
  instanceUrl: string; // Salesforce instance URL
  organizationId: string; // Org ID
  userId: string; // User ID
  accessToken: string; // Current OAuth access token
  refreshToken?: string; // Refresh token (may not be available on all platforms)
}
```

## Complete Login Flow

Here's a complete login flow for Employee Agent with Mobile SDK:

```tsx
import {
  AgentforceService,
  isEmployeeAgentAuthSupported,
  hasEmployeeAgentSession,
  loginForEmployeeAgent,
  getEmployeeAgentCredentials,
} from "@salesforce/react-native-agentforce";

async function launchEmployeeAgent(agentId?: string) {
  // Step 1: Check if auth bridge is available
  const authSupported = await isEmployeeAgentAuthSupported();
  if (!authSupported) {
    throw new Error("Employee Agent auth is not available in this build.");
  }

  // Step 2: Check for existing session, or login
  let creds = await getEmployeeAgentCredentials();
  if (!creds) {
    creds = await loginForEmployeeAgent();
  }

  // Step 3: Get stored agent ID (or use provided one)
  const resolvedAgentId = agentId || (await AgentforceService.getEmployeeAgentId());

  // Step 4: Configure Employee Agent
  await AgentforceService.configure({
    type: "employee",
    instanceUrl: creds.instanceUrl,
    organizationId: creds.organizationId,
    userId: creds.userId,
    agentId: resolvedAgentId || undefined,
    accessToken: creds.accessToken,
  });

  // Step 5: Launch conversation
  await AgentforceService.launchConversation();
}
```

## 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:

```tsx
try {
  const newCreds = await refreshEmployeeAgentCredentials();
  console.log("New access token:", newCreds.accessToken);

  // Optionally reconfigure with new token
  await AgentforceService.configure({
    type: "employee",
    instanceUrl: newCreds.instanceUrl,
    organizationId: newCreds.organizationId,
    userId: newCreds.userId,
    accessToken: newCreds.accessToken,
  });
} catch (error) {
  // Token refresh failed -- may need to re-login
  console.error("Refresh failed:", error);
}
```

## Direct Token Mode

If you don't want to integrate the Mobile SDK but still need Employee Agent functionality, you can provide tokens directly:

```tsx
// Obtain token through your own auth mechanism
const token = await myAuthService.getAccessToken();

await AgentforceService.configure({
  type: "employee",
  instanceUrl: "https://myorg.my.salesforce.com",
  organizationId: "00Dxx0000001234",
  userId: "005xx0000001234",
  agentId: "0Xxxx0000001234",
  accessToken: token,
});

await 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.

Create `src/config/employeeAgentConfig.local.ts`:

```tsx
import type { EmployeeAgentConfig } from "@salesforce/react-native-agentforce";

export const EMPLOYEE_AGENT_ENABLED = true;

export const EMPLOYEE_AGENT_CONFIG: EmployeeAgentConfig = {
  type: "employee",
  instanceUrl: "https://my-dev-org.my.salesforce.com",
  organizationId: "00Dxx0000001234",
  userId: "005xx0000001234",
  accessToken: "dev_access_token_here",
};

export function isEmployeeAgentConfigValid(): boolean {
  return (
    !!EMPLOYEE_AGENT_CONFIG.instanceUrl &&
    !!EMPLOYEE_AGENT_CONFIG.organizationId &&
    !!EMPLOYEE_AGENT_CONFIG.userId
  );
}
```

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`:

```
# Employee Agent local config (contains tokens)
**/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

```tsx
// Is auth available in this build?
const supported = await isEmployeeAgentAuthSupported();

// Is the user currently logged in?
const loggedIn = await hasEmployeeAgentSession();

// Get full credentials (or null)
const creds = await getEmployeeAgentCredentials();
```

### Logout

```tsx
async function handleLogout() {
  // Close any active conversation
  await AgentforceService.closeConversation();

  // Logout via Mobile SDK
  await logoutEmployeeAgent();

  // Reset SDK state
  await AgentforceService.resetSettings();
}
```

### 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.

## See Also

- [Configure the SDK](https://developer.salesforce.com/docs/ai/agentforce-mobile-sdk/guide/agentforce-mobile-sdk-react-native-integration.md)
- [Install the SDK](https://developer.salesforce.com/docs/ai/agentforce-mobile-sdk/guide/agentforce-mobile-sdk-react-native-install.md)
- [Troubleshooting](https://developer.salesforce.com/docs/ai/agentforce-mobile-sdk/guide/agentforce-mobile-sdk-react-native-troubleshooting.md)
