firebase-android-sdk icon indicating copy to clipboard operation
firebase-android-sdk copied to clipboard

`linkWithCredential()` produces "email-already-in-use" exception now when linking EmailAuthProvider with same email address

Open russellwheatley opened this issue 1 year ago • 3 comments

[READ] Step 1: Are you in the right place?

Issues filed here should be about bugs in the code in this repository. If you have a general question, need help debugging, or fall into some other category use one of these other channels:

  • For general technical questions, post a question on StackOverflow with the firebase tag.
  • For general Firebase discussion, use the firebase-talk google group.
  • For help troubleshooting your application that does not fall under one of the above categories, reach out to the personalized Firebase support channel.

[REQUIRED] Step 2: Describe your environment

  • Android Studio version: 2022.3.1
  • Firebase Component: Auth (Database, Firestore, Storage, Functions, etc)
  • Component version: 22.3.0

[REQUIRED] Step 3: Describe the problem

Since firebase-android-sdk BOM 32.5.0 the behaviour of linkWithCredential() has changed, you now get an "email-already-in-use" exception when trying to link an EmailAuthProvider with an existing user with the same email address. I think this might be a breaking change.

This bug was first report on the FlutterFire repo: https://github.com/firebase/flutterfire/issues/12156

Steps to reproduce:

Below is the code for reproduction. A few things to remember:

  1. The google authentication should have the same email address as the one you use in the EmailAuthProvider. the aim is to link the accounts like below: Screenshot 2024-01-23 at 13 41 09

  2. This setting in your Firebase console should be set: Screenshot 2024-01-23 at 14 22 04

Relevant Code:

I have a full reproduction on this branch (it was hacked together for a repro 😓) : https://github.com/russellwheatley/android-storage-emulator/tree/auth-linkWithCredential

For the above, you would have to update:

  1. Use your own Firebase project (i.e replace the google-services.json).
  2. You will also need to change the client id in the strings.xml here: https://github.com/russellwheatley/android-storage-emulator/blob/auth-linkWithCredential/app/src/main/res/values/strings.xml#L9
  3. Also ensure your app has the SHA-1 key in the Firebase console.
  4. Run the app and press the button in the bottom right hand corner.

Alternatively, below is a snippet of the code that is the issue if you wish to drop it in your own project:

FirebaseApp app = FirebaseApp.getInstance();
FirebaseAuth mAuth = FirebaseAuth.getInstance(app);
// You will have to generate your own "idToken" 
AuthCredential firebaseCredential = GoogleAuthProvider.getCredential(idToken, null);
                    mAuth.signInWithCredential(firebaseCredential)
                            .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's information
                                        Log.d(TAG, "signInWithCredential:success");
                                        FirebaseUser user = mAuth.getCurrentUser();

                                        AuthCredential credential = EmailAuthProvider.getCredential("[email protected]", "some-password");

                                        assert user != null;
                                        user.linkWithCredential(credential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                                            @Override
                                            public void onComplete(@NonNull Task<AuthResult> task) {
                                                if (task.isSuccessful()) {
                                                    Log.d(TAG, "onComplete: LINKED");
                                                } else {
                                                    Log.d(TAG, "onComplete: " + Objects.requireNonNull(task.getException()).getMessage());
                                                }
                                            }
                                        });

                                    } else {
                                        // If sign in fails, display a message to the user.
                                        Log.w(TAG, "signInWithCredential:failure", task.getException());
                                    }
                                }
                            });

russellwheatley avatar Jan 23 '24 15:01 russellwheatley

Hi @russellwheatley, thank you for reaching out and reporting the issue. I was able to reproduce the issue with the MCVE you shared. Thank you for that.

Just to share my findings, this issue seems to exists in version 22.3.0 and above. Per checking in version 22.2.0, linkWithCredential() is working as intended.

I’ll notify our engineers and see what we can do here.

lehcar09 avatar Jan 29 '24 18:01 lehcar09

The same problem is happening with Flutter

andrerpena avatar Feb 10 '24 11:02 andrerpena

Thank you for reporting! As mentioned in the iOS bug, we will update here when the fix is released.

NhienLam avatar Feb 12 '24 04:02 NhienLam

+1 We are having the same problem. Downgraded to SDK 10.17.0 temporally. Please let us know when there's any update. Thank you!

yunnnyunnn avatar Feb 22 '24 06:02 yunnnyunnn

Same problem here +1

FujiwaraChoki avatar Feb 29 '24 18:02 FujiwaraChoki

The fix was released. Please let us know if it works for you. Thanks!

NhienLam avatar Mar 11 '24 17:03 NhienLam

I have tested this on FlutterFire, and it is working now 😄. Thank you for the fix!

russellwheatley avatar Mar 12 '24 11:03 russellwheatley

Thank you for confirming @russellwheatley. I'll be closing this thread now. Let us know if we need to reopen the issue. Thanks!.

lehcar09 avatar Mar 12 '24 15:03 lehcar09