将Facebook与具有与Firebase相同电子邮件ID的Google帐户链接

时间:2019-01-06 06:27:16

标签: android firebase firebase-authentication

我正在尝试为我的应用设置登录页面,并希望为用户提供使用电子邮件/密码,Facebook和Google登录的选项。我能够成功登录和注销每种方法。我想用相同的电子邮件地址链接来自不同Auth提供程序的所有帐户。我正在浏览Firebase文档,并通过了linkwithcredential。如果我理解正确,Firebase会告诉您使用任何一种登录方法登录后使用linkwithcredential。在我的情况下,如果我先通过facebbok登录用户,则->用户注销->使用google登录->调用linkwithcredential(我的期望是Facebook在此被链接)。我的理解是否正确?否则,我如何在不要求用户采取任何措施的情况下,使用其他身份验证提供程序平稳地登录用户。请帮助

private void handleFacebookAccessToken(AccessToken token) {
    Log.d(TAG, "handleFacebookAccessToken:" + token);

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

    firebaseAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {

                    if (task.isSuccessful()) {

                        linkCredential(credential);
                        // Sign in success, update UI with the signed-in user's information
                        Log.d(TAG, "signInWithCredential:success");
                        startActivity(new Intent(getApplicationContext(),ProfileActivity.class));

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

                        if(task.getException().toString().equals(getResources().getString(R.
                                string.fb_login_google))){

                            FirebaseUser mUser = firebaseAuth.getCurrentUser();

                            Toast.makeText(MainActivity.this,"User already signed in via google",
                                    Toast.LENGTH_SHORT).show();


                            Log.d("***facebook failure ","failed");
                        }
                        else{
                            Toast.makeText(MainActivity.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }

                    // ...
                }
            });
}

    public void linkCredential(AuthCredential credential){

    Log.d("shitttttt" + FirebaseAuth.getInstance().getCurrentUser(),credential.toString());


    FirebaseAuth.getInstance().getCurrentUser().linkWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        Log.d(TAG, "linkWithCredential:success");

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

                }
            });

    //final AuthCredential fbCredential = FacebookAuthProvider.getCredential(acct.getIdToken());

    Log.d("fbcredential",credential.toString());
}

0 个答案:

没有答案