1

firebaseでfacebook auth medthodを使用しています。ユーザーを正しくログアウトする方法がわかりません。 「Facebookで続行」ボタンを押してプロフィールにアクセスすると、「ログアウト」のボタンが変わり、クリックするとダイアログが表示されます。問題は、それが実際に私をログアウトしないと状態がまだで署名されということである。Android Firebase Facebook認証ログアウト機能

私は私のアプリでFirebaseやFacebookからユーザーをログアウトするhere方法を見つけたが、私はどこ把握することはできませんそれらの2行を置く。私はログアウト機能などを探しています。

私はhere (different here)を見つけました。何が私の解決策かもしれませんが、かなり面倒で理解できません。手伝ってくれませんか?あなたが言及した2つの方法を使用して独自のボタンを実装し、追加する必要があるとOnClickListener

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    mAuth = FirebaseAuth.getInstance(); 

    super.onCreate(savedInstanceState); 
    FacebookSdk.sdkInitialize(getApplicationContext()); 
    AppEventsLogger.activateApp(this); 
    setContentView(R.layout.activity_login); 

    mCallbackManager = CallbackManager.Factory.create(); 
    LoginButton loginButton = (LoginButton) findViewById(R.id.button_facebook_login); 
    loginButton.setReadPermissions("email", "public_profile"); 
    loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() { 
     @Override 
     public void onSuccess(LoginResult loginResult) { 
      Log.e(TAG, "facebook:onSuccess:" + loginResult); 
      handleFacebookAccessToken(loginResult.getAccessToken()); 
     } 

     @Override 
     public void onCancel() { 
      Log.e(TAG, "facebook:onCancel"); 
      // ... 
     } 

     @Override 
     public void onError(FacebookException error) { 
      Log.e(TAG, "facebook:onError", error); 
      // ... 
     } 
    }); 

    mAuthListener = new FirebaseAuth.AuthStateListener() { 
     @Override 
     public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { 
      final FirebaseUser user = firebaseAuth.getCurrentUser(); 
      if (user != null) { 
       // User is signed in 
       Log.e(TAG, "onAuthStateChanged:signed_in:" + user.getUid()); 

       DatabaseReference myRef = database.getReference("Users"); 
       myRef.addValueEventListener(new ValueEventListener() { 
        boolean userExists = false; 
        User userFirebase; 

        @Override 
        public void onDataChange(DataSnapshot dataSnapshot) { 
         for (DataSnapshot mydata : dataSnapshot.getChildren()){ 
          userFirebase = mydata.getValue(User.class); 

          if(userFirebase.getEmail().equals(user.getEmail())){ 
           Log.e(TAG, "User found in the database"); 
           userExists = true; 
           Intent intent = new Intent(getBaseContext(), ActivityMain.class); 
           startActivity(intent); 
           break; 
          } 
         } 

         if (!userExists){ 
          DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference(); 
          mDatabase.child("Users").push().setValue(new User(user.getDisplayName(),user.getEmail(),0,0)); 

          Intent intent = new Intent(getBaseContext(), ActivityMain.class); 
          startActivity(intent); 
         } 
        } 

        @Override 
        public void onCancelled(DatabaseError databaseError) { 

        } 
       }); // checks if the user is in database and writes him if not 

      } else { 
       // User is signed out 
       Log.e(TAG, "onAuthStateChanged:signed_out"); 
      } 
     } 
    }; 


} 

@Override 
public void onStart() { 
    super.onStart(); 
    mAuth.addAuthStateListener(mAuthListener); 
} 

@Override 
public void onStop() { 
    super.onStop(); 
    if (mAuthListener != null) { 
     mAuth.removeAuthStateListener(mAuthListener); 
    } 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    // Pass the activity result back to the Facebook SDK 
    mCallbackManager.onActivityResult(requestCode, resultCode, data); 
} 

private void handleFacebookAccessToken(AccessToken token) { 
    Log.e(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.e(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.e(TAG, "signInWithCredential", task.getException()); 
         Toast.makeText(getBaseContext(), "Authentication failed.", 
           Toast.LENGTH_SHORT).show(); 
        } 

        // ... 
       } 
      }); 
} 

答えて

2

Button logoutButton = (Button) findViewById(R.id.logout_button); 
logoutButton.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     FirebaseAuth.getInstance().signOut(); 
     LoginManager.getInstance().logOut(); 
    } 
}); 

それが唯一のFacebookからログアウトので、あなたは、Facebookのログアウトボタンを使用することはできませんし、あなたもfirebaseログアウトが必要です。

+0

すごいですね!どうもありがとうございます!私はできるだけ早く答えとして受け入れます! –

関連する問題