简体   繁体   中英

Can we link auth providers without user login on Firebase for Android

I signed up with Google. Then I logged out and tried logging in using the same email address as Facebook. As I expected, the user has already registered the error. When I received the error, I gave it the credential to linkWithCredantials method that I got from Facebook. Of course I also got getCurrentUser null error here, because any user logged in. I don't get it well, when will I link auth providers? And my case I want to link auth providers with out login. Is it possible?

PS: Sorry for my English.

Here is my example :

private void handleFacebookAccessToken(final AccessToken token) {

    Log.d(TAG_FACEBOOK, "handleFacebookAccessToken:" + token);

    AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
    final AuthCredential linkCredential = credential;

    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {

                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    Log.d(TAG_FACEBOOK, "signInWithCredential:onComplete:" + task.isSuccessful());

                    if (task.isSuccessful()) {
                        replaceFragment(new ProfileFragment());
                    } else {
                        toast("Facebook authentication failed.");
                    }

                }
            }).addOnFailureListener(getActivity(), new OnFailureListener() {

        @Override
        public void onFailure(@NonNull Exception e) {

            if (e.getMessage().contains("account already exists")) {


                //**here**
                mAuth.getCurrentUser().linkWithCredential(linkCredential)
                        .addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                Log.d(TAG_FACEBOOK, "linkWithCredential:onComplete:" + task.isSuccessful());

                                if (!task.isSuccessful()) {

                                }
                            }
                        });
            }
        }
    });

}

To link two accounts, the user must sign in to both accounts. So they're already signed in as one of the accounts, which is then Firebase Authentication's currently signed in user . Then to link a different account to this, you provide the credentials for that account.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM