14

So I figured out how to properly signout with Google. Cool. Now, what about Facebook?

When I get an error signing in to Facebook, such as an error indicating I already have a Firebase account with the same credentials but a different social provider, I get the "Log out of facebook" button. To be more clear:

I try logging into Facebook, then I get an error (this isn't my problem!), but the problem is, Facebook's button is now on "Log out". When it should still be "Sign In With Facebook". I know why this is happening; it's because the error is with Firebase and Facebook thinks I'm signed in.

But the real question is, how do I properly sign out of Facebook? FirebaseAuth.getInstance().signout() doesn't seem to logout of Facebook itself.

Here's my current logout() method:

static void logOut(final Context context) {
    new SweetAlertDialog(context, SweetAlertDialog.WARNING_TYPE)
        .showCancelButton(true)
        .setTitleText(context.getString(R.string.areYouSure))
        .setContentText(context.getString(R.string.logoutMSG))
        .setCancelText(context.getString(android.R.string.no))
        .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
            @Override
            public void onClick(SweetAlertDialog sweetAlertDialog) {
                sweetAlertDialog.dismiss();
            }
        })
        .setConfirmText(context.getString(R.string.yes))
        .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
            @Override
            public void onClick(final SweetAlertDialog sweetAlertDialog) {
                //region Google
                GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestIdToken(context.getString(R.string.default_web_client_id))
                    .requestEmail()
                    .build();
                final GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(context)
                    .enableAutoManage((FragmentActivity) context, new GoogleApiClient.OnConnectionFailedListener() {
                        @Override
                        public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                            FirebaseCrash.log(connectionResult.getErrorMessage());
                            Toast.makeText(context, context.getString(R.string.error), Toast.LENGTH_SHORT).show();
                        }
                    })
                    .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                    .build();
                mGoogleApiClient.registerConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {@
                    Override
                    public void onConnected(@Nullable Bundle bundle) {

                        FirebaseAuth.getInstance().signOut();
                        if (mGoogleApiClient.isConnected()) {
                            Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(new ResultCallback < Status > () {@
                                Override
                                public void onResult(@NonNull Status status) {
                                    if (status.isSuccess()) {
                                        FirebaseAuth.getInstance().signOut();
                                        sweetAlertDialog.dismiss();
                                        Log.d(TAG, "User Logged out");
                                        Intent intent = new Intent(context, SignUp.class);
                                        context.startActivity(intent);
                                        ((FragmentActivity) context).finish();
                                    } else
                                        Toast.makeText(context, context.getString(R.string.error), Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    }

                    @
                    Override
                    public void onConnectionSuspended(int i) {
                        Log.e(TAG, "Google API Client Connection Suspended");
                    }
                });
                //endregion
            }
        }).show();
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Ali Bdeir
  • 4,151
  • 10
  • 57
  • 117

2 Answers2

65

I had a similar problem and got it solved using both the firebase Auth instance and the facebook LoginManager instance

FirebaseAuth.getInstance().signOut();
LoginManager.getInstance().logOut();

My Question

Community
  • 1
  • 1
Ahmed Ashraf
  • 2,795
  • 16
  • 25
  • Hey, thanks for the answer, but I'm still having a problem. What if the user wants to change his Facebook account? Clicking the "log in" button automatically chooses a user. – Ali Bdeir Sep 11 '16 at 12:53
  • I think if the user changed his account from the facebook app, then clicking login again from your app (while logged out) will redirect him to login with the new account. however, if he still has an active session on your app, you might wanna check for that to do a specific action like in here http://stackoverflow.com/questions/29294015/how-to-check-if-user-is-logged-in-with-fb-sdk-4-0-for-android – Ahmed Ashraf Sep 11 '16 at 13:47
  • I have the same problem. When I logged out, sign out button is still showing up. When I clicked the sign out button, I can log out properly. – Yunus Haznedar Apr 07 '17 at 07:55
  • Where in the class would these two lines of code go? onCreate method? – AndroidDevBro Mar 31 '18 at 18:35
0

3 Sign Out methods for Firebase itself, Google and Facebook.

//LOG_OUT
    public static void signOut(Activity activity) {
        if (mAuth == null)
            mAuth = FirebaseAuth.getInstance();

        //Firebase SignOut
        mAuth.signOut();

        //Google SignOut
        SignInWithGoogle signInWithGoogle = new SignInWithGoogle(activity);
        signInWithGoogle.getGoogleSignInClient().signOut()
                .addOnCompleteListener(activity, new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                Log.d(TAG, "Google Sign Out!!! ");
            }
        });

        //Facebook SignOut
        LoginManager.getInstance().logOut();
    }
Subhojit Halder
  • 145
  • 3
  • 7