Skip to content

Technical Note: Custom Authentication with Android

bensmiley edited this page Feb 27, 2017 · 3 revisions

When the Chat SDK starts up, there are some tasks that need to be complete. These include:

  • Authenticating with Firebase
  • Adding observers to Firebase
  • Configuring the database

If you don't want to use the standard login screen, you will need to trigger these tasks manually using custom authentication.

Here's a snippet which is launched when you click the "Login" button.

// ChatSDKAbstractLoginActivity.java

Map<String, Object> data = AbstractNetworkAdapter.getMap(
        new String[]{BDefines.Prefs.LoginTypeKey, BDefines.Prefs.LoginEmailKey, BDefines.Prefs.LoginPasswordKey},
        BDefines.BAccountType.Password, etEmail.getText().toString(), etPass.getText().toString());

BNetworkManager.sharedManager().getNetworkAdapter()
        .authenticateWithMap(data).done(new DoneCallback<Object>() {
    @Override
    public void onDone(Object o) {
        afterLogin();
    }
}).fail(new FailCallback<BError>() {
    @Override
    public void onFail(BError bError) {
        toastErrorMessage(bError, true);
        dismissProgDialog();
    }
});

We create a new map with some properties and send it to the authenticateWithMap method. If you want to use custom login, you will need to generate a Firebase token on your server and then pass it to the Chat SDK using this method.

First you should look at the Firebase guide to generating custom tokens. This token should be generated on your server and then passed back to your app when the user successfully logs in.

Then the token should be added to a new Hashmap and passed to the authenticateWithMap method. The map should contain the type BDefines.Prefs.Custom and the token keyed by BDefines.Prefs.TokenKey.

This will be handled in the Firebase adapter in the Custom case.

// BFirebaseNetworkAdapter.java

case Custom:

    AbstractNetworkAdapter.provider = BDefines.ProviderString.Custom;

    FirebaseAuth.getInstance().signInWithCustomToken((String) details.get(BDefines.Prefs.TokenKey)).addOnCompleteListener(resultHandler);

    break;

When this method completes, the Chat SDK will be fully setup.