如何使用Firebase在Android上正确退出Facebook?

时间:2016-09-11 09:41:46

标签: android firebase facebook-login firebase-authentication

所以我想出了如何正确地与Google签约。凉。现在,Facebook呢?

当我登录Facebook时出现错误,例如错误表明我已经拥有具有相同凭据但具有不同社交提供商的Firebase帐户,我得到了"退出Facebook"按钮。 更清楚:

我尝试登录Facebook,然后收到错误(这不是我的问题!),但问题是,Facebook的按钮现在开启了#34 ;退出"。当它仍然 "使用Facebook" 登录时。我知道为什么会这样;它的错误在于 Firebase ,Facebook认为我已登录。

但真正的问题是,如何正确退出Facebook? FirebaseAuth.getInstance().signout()似乎没有注销 Facebook 本身。

这是我当前的logout()方法:

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

2 个答案:

答案 0 :(得分:39)

我遇到了类似的问题,并使用firebase Auth实例和facebook LoginManager实例解决了问题

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

My Question

答案 1 :(得分:0)

Firebase 本身、GoogleFacebook 的 3 种退出方法。

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