2016-06-15 69 views
0

對不起,我的英語不好。FireBase Auth結合多個認證系統

我想這2種方法

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());

我藉此從火力地堡文檔結合起來。

我是新的Android的Programmation(我是小C#遊戲開發),所以不要責怪我。

而且我不enderstind這個:https://firebase.google.com/docs/auth/android/account-linking#link-auth-provider-credentials-to-a-user-account

請我能有幫助?

提前致謝。

回答

0

您可以在一個方法中添加參數acct和令牌,並檢查它們是否爲null這樣的private void firebaseAuth(GoogleSignInAccount acct,AccessToken token)。如果acct爲null,那麼用戶應該使用Facebook登錄鑑於您檢查到請求代碼是一個你當你簽署提供在OnActivityResult(int requestCode, int resultCode, Intent data)使用谷歌和你Auth.GoogleSignInApi.getSignInResultFromIntent(data)得到的結果是result.isSuccess() .Same對於Facebook,你從FacebookCallback<LoginResult>()onSuccess()方法傳遞的accessToken,所以你可能會做一些這樣的事:

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 
      } 

     } 
}