Authentication and Authorization in Android (APAC)

Learn about authentication using the Brand Messenger Android SDK

This section will guide you in authenticating users for the pre-built customizable chat UI in your Android app which can be a Java or Kotlin android project.

SDK Initialization

Initialize the SDK using the Company Key and Application Id:

BrandMessengerManager.init(this, BrandMessenger.getInstance(this).getCompanyKey(), BrandMessenger.getInstance(this).getApplicationKey());
BrandMessengerManager.init(context, BrandMessenger.getInstance(context).companyKey, BrandMessenger.getInstance(context).applicationKey)

User Registration/Login

Optional region setting

The SDK will authenticate with Khoros Auth Handler endpoint during login. Depending on the region, the customer can set the region. Options are "US" or "APAC". Default is "APAC" when not set.

BrandMessengerManager.setRegion(context, "US");
BrandMessengerManager.setRegion(context, "US")

🚧

Note

User logins are persistent. Once a user is logged in, they won't be required to log in again until and unless they have logged out.

You can check if the user's login status to Brand Messenger using the following:

if (BrandMessengerManager.isAuthenticated(context, false)){
  //do something
}
if (BrandMessenger.isAuthenticated(context, false)) {
    //do something
}

Here is how to log in to Brand Messenger using an access token:

BrandMessengerManager.login(context, "access_token", new KBMLoginHandler() {
    @Override
    public void onSuccess(@NonNull RegistrationResponse registrationResponse, @Nullable Context context) {
    }
    @Override
    public void onFailure(@Nullable RegistrationResponse registrationResponse, @Nullable Exception exception) {
    }
})
BrandMessengerManager.login(context, "access_token", object : KBMLoginHandler {
    override fun onSuccess(registrationResponse: RegistrationResponse, context: Context?) {
    }
    override fun onFailure(registrationResponse: RegistrationResponse?, exception: Exception?) {
    }
}

In the event that this is a new user, a new user account is created. Existing users are logged in to the application.

You can perform further actions based on the callback methods. In onSuccess you could launch the chat screen for the user, onFailure you could throw some error message based on the exception and the response received in the callback method.

📘

Note

You need to call the login method only once. However, the method internally checks if the user is logged in, if he/she is already logged in you would still receive the RegistrationResponse in onSuccess callback with a "User already logged in" message.

Authentication Delegate

When the auth token expires and SDK's refresh/token API fails, the sdk offers a way to re-establish login auth during operation via a delegate object.

BrandMessenger.getInstance(context).setAuthenticationDelegate(new KBMAuthenticationDelegate() {
    @Override
    public void onRefreshFail(KBMAuthenticationDelegateCallback callback) {
        callback.updateToken(<accesstoken>);
    }
});
BrandMessenger.getInstance(context).setAuthenticationDelegate { object: KBMAuthenticationDelegate {
    override fun onRefreshFail(callback: KBMAuthenticationDelegateCallback?) {
        callback?.updateToken(<accesstoken>)
    }

}}

On onRefreshFail, the application can generate a new accesstoken and pass it back in callback.updateToken. The SDK will re-login and continue operation.

Alternate Auth flow using Customer iDP

13321332

Did this page help you?