【问题标题】:FireBase Auth Combine multiple auth systemFireBase Auth 组合多个认证系统
【发布时间】:2016-06-15 06:36:41
【问题描述】:

对不起,我的英语不好。

我想结合这两种方法

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 编程新手(我是小 C# 游戏开发人员),所以不要怪我。

我不同意这个:https://firebase.google.com/docs/auth/android/account-linking#link-auth-provider-credentials-to-a-user-account

请问我可以帮忙吗?

提前致谢。

【问题讨论】:

    标签: java android authentication firebase


    【解决方案1】:

    您可以在一种方法中同时添加参数 acct 和 token 并检查它们中的每一个是否是 null 就像这样 private void firebaseAuth(GoogleSignInAccount acct,AccessToken token)。如果 acct 为空,那么用户应该使用 facebook 登录假设您检查了请求代码是您在 OnActivityResult(int requestCode, int resultCode, Intent data) 中使用 Google 登录时提供的代码,您从 Auth.GoogleSignInApi.getSignInResultFromIntent(data) 获得的结果是 result.isSuccess()。当您从 onSuccess()onSuccess() 方法传递 AccessToken 时,Facebook 的代码相同,所以您可能会做这样的事情:

    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
                }
    
           }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-10-17
      • 2023-04-07
      • 1970-01-01
      • 2017-12-02
      • 2020-05-13
      • 1970-01-01
      • 2011-04-16
      • 2012-06-30
      • 2011-10-25
      相关资源
      最近更新 更多