Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Phone Authentication credentials linking with Google login in firebase

I have implemented two step authentication in my app using firebase authentication in which I have used gmail, facebook or simple email login for authenticating. As digits phone verification has migrated to firebase i have implemented firebase phone authentication by linking the existing logged in account (facebook, gmail, or email) with phone authentication credentials. It works fine when used with facebook and email account. When user is logged in through google and tries to verify mobile through phone authentication this logs are printed :

signInWithCredential:failure

com.google.firebase.auth.FirebaseAuthUserCollisionException: An account already exists with the same email address but different sign-in credentials. Sign in using a provider associated with this email address.

Read this article. Is it the same issue as mentioned in the article?? Is there any solution for the same..

like image 661
Ashish Pardhiye Avatar asked Jun 14 '17 10:06

Ashish Pardhiye


People also ask

How do I get Firebase auth credentials?

To sign a user into your app, you first get authentication credentials from the user. These credentials can be the user's email address and password, or an OAuth token from a federated identity provider. Then, you pass these credentials to the Firebase Authentication SDK.

How do I use Firebase authentication on my phone?

Create fictional phone numbers and verification codes In the Firebase console, open the Authentication section. In the Sign in method tab, enable the Phone provider if you haven't already. Open the Phone numbers for testing accordion menu. Provide the phone number you want to test, for example: +1 650-555-3434.


1 Answers

After research over internet and in firebase documentation itself I found solution to this two step authentication in app using firebase auth.

firebaseAuth.getCurrentUser().updatePhoneNumber(credential).addOnCompleteListener(this, new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
            if (task.isSuccessful()) {
                Log.d(TAG, "signInWithCredential:success");

                Snackbar.make(findViewById(android.R.id.content), "Mobile Verified Successfully.",
                        Snackbar.LENGTH_SHORT).show();

            } else {
                Log.w(TAG, "signInWithCredential:failure", task.getException());
                if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                    //mVerificationField.setError("Invalid code.");
                    Snackbar.make(findViewById(android.R.id.content), "Invalid Code.",
                            Snackbar.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(context,"signInWithCredential:failure"+task.getException(),
                            Snackbar.LENGTH_LONG).show();
                }
            }
        }
    });

Just pass the PhoneAuthCredential to above method and it will verify the phone assigns to your existing account. Make sure it is not used by any other account.

PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
like image 135
Ashish Pardhiye Avatar answered Oct 12 '22 23:10

Ashish Pardhiye