Search code examples
androidfirebasefirebase-authenticationtokendigits

Firebase phone auth response with null token


I am migrating the Digits authentication to Firebase authentication following these official links:

google-services.json file exported from Firebase console is integrated on the project.

I am using the drop-in solution with UI and this is my code:

startActivityForResult(
                AuthUI.getInstance()
                        .createSignInIntentBuilder()
                        .setProviders(Arrays.asList(
                                new AuthUI.IdpConfig.Builder(AuthUI.PHONE_VERIFICATION_PROVIDER).build())).build(), RC_SIGN_IN);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_SIGN_IN) {
        IdpResponse response = IdpResponse.fromResultIntent(data);

        // Successfully signed in
        if (resultCode == RESULT_OK) {
            IdpResponse idpResponse = IdpResponse.fromResultIntent(data);

            //Here retreive the token from the response
            idpResponse.getIdpToken();
            finish();
            return;
        } else {
            // Sign in failed
            if (response == null) {
                // User pressed back button
                return;
            }

            if (response.getErrorCode() == ErrorCodes.NO_NETWORK) {
                Toast.makeText(this, getString(R.string.connection_error), Toast.LENGTH_SHORT).show();
                return;
            }

            if (response.getErrorCode() == ErrorCodes.UNKNOWN_ERROR) {
                Toast.makeText(this, getString(R.string.error_default), Toast.LENGTH_SHORT).show();
                return;
            }
        }

        Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
    }
}

And this is the interesting part of the build.gradle:

android {
    compileSdkVersion 26
    buildToolsVersion '26.0.1'

}

compile 'com.firebaseui:firebase-ui-auth:2.3.0'
compile 'com.android.support:appcompat-v7:26.0.1'
compile 'com.android.support:support-v4:26.0.1'
compile 'com.android.support:recyclerview-v7:26.0.1'
compile 'com.android.support:design:26.0.1'
compile 'com.android.support:support-core-utils:26.0.1'
compile 'com.android.support:support-annotations:26.0.1'
compile 'com.android.support:percent:26.0.1'

When onActivityResult is fired, the response is successful but the token always is null.

I don't know if with this authentication provider, this token it's always null or I'm doing something wrong.


Solution

  • You'll need to explicitly request for the token.

    See https://firebase.google.com/docs/auth/admin/verify-id-tokens:

    FirebaseUser mUser = FirebaseAuth.getInstance().getCurrentUser();
    mUser.getToken(true)