Authenticating Using the Salesforce Mobile SDK
These instructions describe how to authenticate the Service Chat SDK using the
provided authentication mechanism within the Salesforce Mobile SDK.
Before starting, make sure that you’ve already:
- Installed the Service Chat SDK. See Install the Service SDK for Android.
- Installed the Salesforce Mobile SDK. If you want to install this SDK using maven, add a dependency on com.salesforce.mobilesdk:SalesforceSDK:<VERSION_NUMBER> to your module's build.gradle file.
- Created a connected app that allows the SDK to authenticate with your Salesforce Experience Cloud site. See Connected Apps. For an overview of Salesforce authentication from a mobile device, see Understand Security and Authentication.
-
If you're using a Salesforce Experience Cloud site, be sure to configure the login
endpoint as described in the Salesforce Mobile SDK documentation (Configure the Login
Endpoint).
The documentation describes how to use the first server listed in your servers.xml file as the default login location. For example:
1<servers> 2 <server name="Site Login" url="https://MY_SITE_URL.com"/> 3</servers> -
Add an account_type property to strings.xml. The property must be unique to your app to
prevent conflicts with other apps that use the Service Chat SDK or the Salesforce Mobile
SDK.
1<string name="account_type">com.mycompany.myapp</string> -
Implement AuthenticatedUser as a wrapper to your Mobile SDK user.
In Java:
1import com.salesforce.androidsdk.accounts.UserAccount; 2import com.salesforce.android.service.common.http.AuthenticatedUser; 3 4public class MobileSdkUser implements AuthenticatedUser { 5 private final String mUserId; 6 7 public MobileSdkUser (UserAccount userAccount) { 8 mUserId = userAccount.getUserId(); 9 } 10 11 @Override public String getUserId () { 12 return mUserId; 13 } 14}In Kotlin:
1class MobileSdkUser(userAccount: UserAccount) : AuthenticatedUser { 2 private val mUserId: String 3 4 init { 5 mUserId = userAccount.getUserId() 6 } 7 8 override fun getUserId(): String { 9 return mUserId 10 } 11} -
Implement AuthTokenProvider as a wrapper to your Mobile SDK authentication
system.
In Java:
1import android.support.annotation.NonNull; 2import android.support.annotation.Nullable; 3import com.salesforce.android.service.common.http.AuthTokenProvider; 4import com.salesforce.android.service.common.http.ResponseSummary; 5import com.salesforce.androidsdk.rest.ClientManager; 6 7public class MobileSdkAuthTokenProvider implements AuthTokenProvider { 8 9 private final ClientManager.AccMgrAuthTokenProvider mTokenProvider; 10 private String mAuthToken; 11 12 public MobileSdkAuthTokenProvider (ClientManager.AccMgrAuthTokenProvider tokenProvider, 13 String initialToken) { 14 mTokenProvider = tokenProvider; 15 mAuthToken = initialToken; 16 } 17 18 @Nullable @Override public String getToken () { 19 return mAuthToken; 20 } 21 22 @Nullable @Override public String getTokenType () { 23 return "Bearer"; 24 } 25 26 @Override public boolean canRefresh () { 27 return true; 28 } 29 30 @Override public void refreshToken (@NonNull ResponseSummary responseSummary) { 31 mAuthToken = mTokenProvider.getNewAuthToken(); 32 } 33}In Kotlin:
1class MobileSdkAuthTokenProvider( 2 private val mTokenProvider: ClientManager.AccMgrAuthTokenProvider, 3 private var mAuthToken: String?) : AuthTokenProvider { 4 5 override fun getToken(): String? { 6 return mAuthToken 7 } 8 9 override fun getTokenType(): String? { 10 return "Bearer" 11 } 12 13 override fun canRefresh(): Boolean { 14 return true 15 } 16 17 override fun refreshToken(responseSummary: ResponseSummary) { 18 mAuthToken = mTokenProvider.getNewAuthToken() 19 } 20} -
When your app launches, initialize the Salesforce Mobile SDK.
1// Initialize the Salesforce Mobile SDK 2SalesforceSDKManager.initNative(context, null, MainActivity.class);For more information, see the SalesforceSDKManager documentation in the Mobile SDK Developer’s Guide.
-
Write code that allows a user to log in and log out of their org using the Salesforce
Mobile SDK. Make sure the user logs in before showing the Service Chat SDK UI.
For example:
1public void login (final Activity activity) { 2 SalesforceSDKManager.getInstance() 3 .getClientManager() 4 .getRestClient(activity, 5 new ClientManager.RestClientCallback() { 6 7 @Override public void authenticatedRestClient (RestClient client) { 8 // Handle authenticated state activity here 9 } 10 }); 11} 12 13public void logout (final Activity activity) { 14 SalesforceSDKManager.getInstance().logout(activity, true); 15} -
When configuring Knowledge (using KnowledgeConfiguration) or Case Management (using CaseConfiguration), use the withAuthConfig method in the builder and pass in your implementation for AuthenticatedUser and AuthTokenProvider.
This code sample illustrates how it works with Knowledge:
1// Create Knowledge configuration builder 2KnowledgeConfiguration.Builder builder = 3 KnowledgeConfiguration.builder(siteUrl).offlineResourceConfig(offlineConfig); 4 5// Grab a user account from the Salesforce Mobile SDK 6UserAccount userAccount = 7 SalesforceSDKManager.getInstance().getUserAccountManager().getCurrentUser(); 8 9if (userAccount != null) { 10 11 // Create an authorization token provider from the Salesforce Mobile SDK 12 ClientManager.AccMgrAuthTokenProvider authTokenProvider = 13 new ClientManager.AccMgrAuthTokenProvider( 14 SalesforceSDKManager.getInstance().getClientManager(), 15 userAccount.getInstanceServer(), 16 userAccount.getAuthToken(), 17 userAccount.getRefreshToken()); 18 19 // Create a wrapper with the Salesforce Mobile SDK token provider 20 AuthTokenProvider provider = 21 new MobileSdkAuthTokenProvider(authTokenProvider, 22 userAccount.getAuthToken()); 23 24 // Build a config using the token provider and the authenticated user 25 builder.withAuthConfig(provider, new MobileSdkUser(userAccount)); 26} 27 28// And now build a configuration object! 29KnowledgeConfiguration config = builder.build();This code sample illustrates how it works with Case Management:
1// Create Case Management configuration builder 2CaseConfiguration.Builder builder = 3 new CaseConfiguration.Builder(SITE_URL, CREATE_CASE_ACTION_NAME) 4 5// Grab a user account from the Salesforce Mobile SDK 6UserAccount userAccount = 7 SalesforceSDKManager.getInstance().getUserAccountManager().getCurrentUser(); 8 9if (userAccount != null) { 10 11 // Create an authorization token provider from the Salesforce Mobile SDK 12 ClientManager.AccMgrAuthTokenProvider authTokenProvider = 13 new ClientManager.AccMgrAuthTokenProvider( 14 SalesforceSDKManager.getInstance().getClientManager(), 15 userAccount.getInstanceServer(), 16 userAccount.getAuthToken(), 17 userAccount.getRefreshToken()); 18 19 // Create a wrapper with the Salesforce Mobile SDK token provider 20 AuthTokenProvider provider = 21 new MobileSdkAuthTokenProvider(authTokenProvider, 22 userAccount.getAuthToken()); 23 24 // Build a config using the token provider and the authenticated user 25 builder.withAuthConfiguration(provider, new MobileSdkUser(userAccount)) 26 .caseListName(CASE_LIST_NAME); 27} 28 29// Create a core configuration instance 30CaseConfiguration coreConfiguration = builder.build();
At this point, you can use the features of the Service Chat SDK as an authenticated user.