Firebase Phone Authentication Accounts Associated with Google Firebase Login

I performed two-step authentication in my application using firebase check, in which I used gmail, facebook or a simple email login for authentication. When the digital verification of the phone was transferred to firebase, I applied firebase phone authentication by linking an existing account (facebook, gmail or email) with the phone authentication credentials. It works great when used with facebook and email. When a user logs in via Google and tries to verify mobility through phone authentication, these logs are printed:

signInWithCredential: malfunction

com.google.firebase.auth.FirebaseAuthUserCollisionException: An account already exists with the same email address, but with different login credentials. Log in using the provider associated with this email address.

Read the article. This is the same as mentioned in the article. Is there any solution for the same.

+5
source share
3 answers

After researching the internet and the firebase documentation, I found a solution for this two-step authentication in an application 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 confirm that the phone is assigning your existing account. Make sure it is not used by any other account.

 PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code); 
+1
source

now auth phone is available in firebase. Here is the code for Auth phone using Firebase: If you have any questions, ask me for free.

 EditText phoneNum,Code; //// two edit text one for enter phone number other for enter OTP code Button sent_,Verify; // sent_ button to request for verification and verify is for to verify code private PhoneAuthProvider.ForceResendingToken mResendToken; private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks; private FirebaseAuth mAuth; private String mVerificationId; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_phone_number_auth); phoneNum =(EditText) findViewById(R.id.fn_num); Code =(EditText) findViewById(R.id.code); sent_ =(Button)findViewById(R.id.sent_nu); Verify =(Button)findViewById(R.id.verify); callback_verificvation(); mAuth = FirebaseAuth.getInstance(); sent_.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String num=phoneNum.getText().toString(); startPhoneNumberVerification(num); // call function for receive OTP 6 digit code } }); Verify.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String code=Code.getText().toString(); verifyPhoneNumberWithCode(mVerificationId,code); //call function for verify code } }); } private void startPhoneNumberVerification(String phoneNumber) { // [START start_phone_auth] PhoneAuthProvider.getInstance().verifyPhoneNumber( phoneNumber, // Phone number to verify 60, // Timeout duration TimeUnit.SECONDS, // Unit of timeout this, // Activity (for callback binding) mCallbacks); // OnVerificationStateChangedCallbacks // [END start_phone_auth] } private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) { mAuth.signInWithCredential(credential) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { // Sign in success, update UI with the signed-in user information FirebaseUser user = task.getResult().getUser(); Toast.makeText(getApplicationContext(), "sign in successfull", Toast.LENGTH_SHORT).show(); // [START_EXCLUDE] // [END_EXCLUDE] } else { // Sign in failed, display a message and update the UI if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) { // The verification code entered was invalid // [START_EXCLUDE silent] // [END_EXCLUDE] } // [START_EXCLUDE silent] // Update UI // [END_EXCLUDE] } } }); } private void verifyPhoneNumberWithCode(String verificationId, String code) { // [START verify_with_code] PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code); // [END verify_with_code] signInWithPhoneAuthCredential(credential); } private void callback_verificvation() { mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() { @Override public void onVerificationCompleted(PhoneAuthCredential credential) { signInWithPhoneAuthCredential(credential); } @Override public void onVerificationFailed(FirebaseException e) { // This callback is invoked in an invalid request for verification is made, } @Override public void onCodeSent(String verificationId, PhoneAuthProvider.ForceResendingToken token) { // The SMS verification code has been sent to the provided phone number, we // now need to ask the user to enter the code and then construct a credential // by combining the code with a verification ID. // Save verification ID and resending token so we can use them later mVerificationId = verificationId; mResendToken = token; } }; 
0
source

This exception is caused due to the fact that you connected the email login and the email login email together google account with the same email address that is used in facebook is not connected together. By default, firebase does not allow multiple accounts from the same email address, this collision occurs.

You have two options to solve this problem.

1. Link your google account with facebook and email it using

 mAuth.getCurrentUser().linkWithCredential(credential); 

Adding new credentials to existing registered users.

2. Enable multiple accounts from the same email (not recommended) from the firebase console

This will make the new uid for the user registered on Google, and the previous facebook login user will have the old one.

-1
source

Source: https://habr.com/ru/post/1268840/


All Articles