0

In the Firebase documentation on linking different authentication methods to the same user account, step 4 is confusing me. As I understand it, to login with a different auth provider, one would have to be logged out of the existing auth provider. However, how can we then call mAuth.getCurrentUser() to link the new auth with the existing one?

Step 4: Pass the AuthCredential object to the signed-in user's linkWithCredential method:

mAuth.getCurrentUser().linkWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            Log.d(TAG, "linkWithCredential(FB):success");
                            FirebaseUser currentUser = task.getResult().getUser();
                            login(currentUser);

                        } else {
                            Log.w(TAG, "linkWithCredential:failure", task.getException());
                            Toast.makeText(MainActivity.this, "Firebase Authentication failed.",
                                    Toast.LENGTH_SHORT).show();

                        }

                        // ...
                    }
                });

When I try run it as per the docs, I get a nullpointer exception, which is understandable because an instance of mAuth hasn't been created at this point.

Christopher Mills
  • 711
  • 10
  • 28
  • you have to first register/sign-in user as single sign-in and then you can add this code to implement multiple sign-in. – Rahul Nov 08 '17 at 08:01
  • Are you saying that I must offer an option to users to link other accounts, once they are signed in? – Christopher Mills Nov 08 '17 at 08:07
  • it depends. but as per oauth, you will have to show user some dialog to let them give your app permission to login via them otherwise, you won't be getting token for that particular credential. also, facebook and google sign-in does not exists side by side for same user due to the fact that facebook does not guarantee the email used for account is secured. (Google uses gmail) – Rahul Nov 08 '17 at 08:11
  • How would I know to prompt a user to link an account. Example: They login with Google on day 1. On day 2 they select to login with FB. How do I prompt to link the FB account with the same user that logged in with Google? One option is to always prompt a user to link other accounts, but that could become irritating to the user. – Christopher Mills Nov 08 '17 at 08:21
  • yes I know but afaik, you cannot link account signed in with google to facebook. – Rahul Nov 08 '17 at 08:24
  • Firebase uses the two methods side by side in the example in the docs, so it should be possible. – Christopher Mills Nov 08 '17 at 08:30
  • Possible duplicate of [Android Firebase linking multiple account providers with matching email](https://stackoverflow.com/questions/44125583/android-firebase-linking-multiple-account-providers-with-matching-email) – Christopher Mills Nov 08 '17 at 09:50
  • @ChristopherMills Hi, were you able to solve this? – Zayid Mohammed Nov 10 '17 at 12:33
  • @ZayidMohammed I've done quite a bit of reading so far but I've yet to implement. Basically, in Firebase, if the Facebook email is the same as the Google one, Google will log into an account that was previously authenticated using Facebook, but not visa versa (note this this is not actually linking, but rather one auth method overriding another). Otherwise, linking would have to be done manually from within any given signed in instance (think duplicating the login buttons, but calling `linkWithCredential` instead of `signInWithCredential`. Will post my results once I have some. – Christopher Mills Nov 10 '17 at 16:17
  • @ChristopherMills thanks for the information. :-) – Zayid Mohammed Nov 13 '17 at 05:11

0 Answers0