2016-06-15 11 views
0

Sorry für mein schlechtes Englisch.FireBase Auth Kombinieren mehrere Auth-System

Ich will Änderung

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

        // ... 
       } 
      }); 
} 

Nur diese Linie, die diese zwei Methoden kombinieren AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null); FacebookAuthProvider.getCredential(token.getToken());

ich dies von Firebase Dokumentation nehmen.

Ich bin neu auf Android-Programmierung (ich bin kleine C# Spiel-dev), also beschuldige mich nicht.

Und ich dies nicht enderstind: https://firebase.google.com/docs/auth/android/account-linking#link-auth-provider-credentials-to-a-user-account

Bitte kann ich Hilfe bekommen?

Vielen Dank im Voraus.

Antwort

0

Sie können beide Parameter acct hinzufügen und in einem Verfahren Token und überprüfen, ob jeder von ihnen ist null wie diese private void firebaseAuth(GoogleSignInAccount acct,AccessToken token) .Wenn acct null ist dann der Benutzer Zeichen dafür sein, sollte sich mit der Vorausgesetzt, dass Sie überprüft, dass die Anforderungs-Code das ist ein bieten Sie, wenn Sie sich mit der Google melden Sie sich an OnActivityResult(int requestCode, int resultCode, Intent data) und das Ergebnis, das Sie von Auth.GoogleSignInApi.getSignInResultFromIntent(data) bekam ist result.isSuccess() .Same für Facebook, wie Sie die AccessToken von onSuccess() Methode des FacebookCallback<LoginResult>() so passieren können Sie etwas tun, wie folgt:

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

und im Falle von Google Anmelden:

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 
      } 

     } 
}