Firebase Authentication - Initializing Multiple Pr

2019-08-22 04:08发布

问题:

Expected

  • Initialize multiple Firebase projects for the Coinverse Android app to allow users to download the open sourced GitHub project as outlined in this StackOverflow answer.
  • Allow users to setup their own Firebase project for authentication
  • Run the open build variant of the app and login with Firebase AuthUI
  • Enable a second pre-set Firebase project for shared Firestore and Cloud Functions

Observed

Auth for one Firebase projects works, throws error below when initializing a second project

 fun initialize(context: Context) {
    if (BuildConfig.BUILD_TYPE == open.name)
        FirebaseApp.initializeApp(
                context,
                FirebaseOptions.Builder()
                        .setApplicationId(APP_ID_OPEN)
                        .setApiKey(APP_API_KEY_OPEN)
                        .setDatabaseUrl(DATABASE_URL_OPEN)
                        .setProjectId(PROJECT_ID_OPEN)
                        .setStorageBucket(STORAGE_BUCKET_OPEN)
                        .build(),
                open.name)
    Firebase.setAndroidContext(context)
    initializeRemoteConfig()
}

Auth for anonymous user works when initializing multiple Firebase projects

Error when initializing two Firebase projects and attempting Google sign in.

Sign in fail com.firebase.ui.auth.FirebaseUiException: Code: 10, message: 10:

Full Log

2019-07-29 16:34:08.130 12406-12406/app.coinverse.open E/AuthUI: A sign-in error occurred.
com.firebase.ui.auth.FirebaseUiException: Code: 10, message: 10: 
    at com.firebase.ui.auth.data.remote.GoogleSignInHandler.onActivityResult(GoogleSignInHandler.java:109)
    at com.firebase.ui.auth.ui.idp.SingleSignInActivity.onActivityResult(SingleSignInActivity.java:128)
    at android.app.Activity.dispatchActivityResult(Activity.java:7462)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4391)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4440)
    at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6718)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-07-29 16:34:08.134 12406-12406/app.coinverse.open E/AuthUI: A sign-in error occurred.
com.firebase.ui.auth.FirebaseUiException: Code: 10, message: 10: 
    at com.firebase.ui.auth.data.remote.GoogleSignInHandler.onActivityResult(GoogleSignInHandler.java:109)
    at com.firebase.ui.auth.ui.idp.SingleSignInActivity.onActivityResult(SingleSignInActivity.java:128)
    at android.app.Activity.dispatchActivityResult(Activity.java:7462)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4391)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4440)
    at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6718)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-07-29 16:34:08.211 12406-12406/app.coinverse.open E/AuthUI: A sign-in error occurred.
com.firebase.ui.auth.FirebaseUiException: Code: 10, message: 10: 
    at com.firebase.ui.auth.data.remote.GoogleSignInHandler.onActivityResult(GoogleSignInHandler.java:109)
    at com.firebase.ui.auth.ui.idp.SingleSignInActivity.onActivityResult(SingleSignInActivity.java:128)
    at android.app.Activity.dispatchActivityResult(Activity.java:7462)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4391)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4440)
    at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6718)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-07-29 16:34:08.261 12406-12406/app.coinverse.open E/SignInDialogFragment: Sign in fail com.firebase.ui.auth.FirebaseUiException: Code: 10, message: 10: 

Implementation

MainActivity

if (BuildConfig.BUILD_TYPE == open.name) {
        //Configures user's Firebase project.
        FirebaseApp.initializeApp(
                context,
                FirebaseOptions.Builder()
                        .setApplicationId(APP_ID_OPEN_PRIVATE)
                        .setApiKey(APP_API_KEY_OPEN_PRIVATE)
                        .setDatabaseUrl(DATABASE_URL_OPEN_PRIVATE)
                        .setProjectId(PROJECT_ID_OPEN_PRIVATE)
                        .build(),
                OPEN_PRIVATE)
        //Configures shared pre-set Firebase project.
        FirebaseApp.initializeApp(
                context,
                FirebaseOptions.Builder()
                        .setApplicationId(APP_ID_OPEN_SHARED)
                        .setApiKey(APP_API_KEY_OPEN_SHARED)
                        .setDatabaseUrl(DATABASE_URL_OPEN_SHARED)
                        .setProjectId(PROJECT_ID_OPEN)
                        .setStorageBucket(STORAGE_BUCKET_OPEN_SHARED)
                        .build(),
                open.name)
    }
    Firebase.setAndroidContext(context)
    initializeRemoteConfig()
}
// If build type is not 'open' configures Firebase Project from google-services.json in 'debug' src set.

SignInFragment

//Returns the relevant Firebase project.
fun firebaseApp(isOpenPrivateUser: Boolean)=
    if (BuildConfig.BUILD_TYPE != open.name) FirebaseApp.getInstance()
    else if (BuildConfig.BUILD_TYPE == open.name && isOpenPrivateUser)
            FirebaseApp.getInstance(OPEN_PRIVATE)
    else FirebaseApp.getInstance(open.name)

confirm.setOnClickListener {
    startActivityForResult(
        AuthUI.getInstance(firebaseApp(true)).createSignInIntentBuilder()
                .setAvailableProviders(listOf(AuthUI.IdpConfig.GoogleBuilder().build()))
                .build(),
            RC_SIGN_IN)
    }

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == RC_SIGN_IN)
        if (resultCode == Activity.RESULT_OK) {
            homeViewModel.setUser(getInstance().currentUser)
            dismiss()
        } else {
            Log.e(LOG_TAG, "Sign in fail ${IdpResponse.fromResultIntent(data)?.error}")
        }
}

回答1:

Solutions

Issue 1

Each package may only be associated with one SHA-1 key. When removing the additional Firebase project and reverting the shared Firebase project to it's original state with the SHA-1 key the console provided the following error which revealed an issue.

An OAuth2 client already exists for this package name and SHA-1 in another project

This error message did not show originally, and would be useful to see the first time the same SHA-1 key is detected in a Firebase project with the same Android package name.

Solution

Remove the SHA-1 key from any other Firebase projects with the same package name. When using two Firebase projects in an Android application, they cannot use the same SHA-1 key.

Issue 2

The default Firebase project was not being generated from the google-services.json in the second newly created project.

Solution

Rebuild the project in Android Studio under Build > Rebuild Project