FireBase Auth组合多个身份验证系统

时间:2016-06-15 06:36:41

标签: java android authentication firebase

抱歉我的英语不好。

我想结合这两种方法

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());

                    // If sign in fails, display a message to the user. If sign in succeeds
                    // the auth state listener will be notified and logic to handle the
                    // signed in user can be handled in the listener.
                    if (!task.isSuccessful()) {
                        Log.w(TAG, "signInWithCredential", task.getException());
                        Toast.makeText(MainActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show();
                    }
                }
            });
}

private void firebaseAuthWithFacebook(AccessToken token) {
    Log.d(TAG, "handleFacebookAccessToken:" + token);
    AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
                    // If sign in fails, display a message to the user. If sign in succeeds
                    // the auth state listener will be notified and logic to handle the
                    // signed in user can be handled in the listener.
                    if (!task.isSuccessful()) {
                        Log.w(TAG, "signInWithCredential", task.getException());
                        Toast.makeText(MainActivity.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();
                    }

                    // ...
                }
            });
}

只有这一行改变了 AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null); FacebookAuthProvider.getCredential(token.getToken());

我从FireBase文档中获取此信息。

我是Android Programmation的新手(我是小C#游戏开发者)所以不要怪我。

我没有理解这一点:https://firebase.google.com/docs/auth/android/account-linking#link-auth-provider-credentials-to-a-user-account

我可以帮忙吗?

提前感谢。

1 个答案:

答案 0 :(得分:0)

你可以在一个方法中添加参数acct和token,并检查每个参数是否nullprivate void firebaseAuth(GoogleSignInAccount acct,AccessToken token)。如果acct为null,那么用户应该使用facebook登录鉴于你检查请求代码是您在OnActivityResult(int requestCode, int resultCode, Intent data)中使用Google登录时提供的请求代码,而Auth.GoogleSignInApi.getSignInResultFromIntent(data)获得的结果是result.isSuccess()。与从onSuccess()传递AccessToken的Facebook相同{1}} FacebookCallback<LoginResult>()的方法,所以你可以这样做:

CallbackManager mCallbackManager = CallbackManager.Factory.create();
        LoginManager.getInstance().registerCallback(mCallbackManager,
                new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                Log.d(TAG, "facebook:onSuccess:" + loginResult);

                firebaseAuth(null,loginResult.getAccessToken);
               //null here is the google account as you sign in using facebook            
            }

            @Override
            public void onCancel() {
                //Something to do with user cancel login
            }

            @Override
            public void onError(FacebookException error) {
                //Something to do with FacebookException
            }
        });

如果是Google登录:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
        if (requestCode == GOOGLE_SIGN_IN) {

        /* request code you pass to 
         * startActivityForResult(intent, GOOGLE_SIGN_IN)
         * where intent is what you get from intent = 
         * Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient)
         */ 

        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);    

            if (result.isSuccess()) {
                // Google Sign In was successful, authenticate with Firebase
                GoogleSignInAccount account = result.getSignInAccount();
                firebaseAuth(account,null);
                /* null here is the facebook token 
                 * as you sign in using Google
                 */

            } else if(result.getStatus().isInterrupted()){
                // Google Sign In failed, update UI appropriately
            }

       }
}