简体   繁体   中英

How to implement single logout function for Facebook and Google Authentication

I am implementing Facebook and Google Authentication in my android application.

I have completed the authentication for both of them, but for logging out from the application I have a single button and there are two very different approach for logging out either by facebook or by google.

I need to logout from the application, considering from which account user logged in to my application, ie Facebook or Google.

Below is the code for logging out from Facebook:

FacebookSdk.sdkInitialize(getApplicationContext());
LoginManager.getInstance().logOut();

And, this is for google:

private void signOut() {
    Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
            new ResultCallback<Status>() {
                @Override
                public void onResult(Status status) {
                    // ...
                }
            });
}

How to implement these functions according to types of accounts that the user has logged on to.

Thanks in advance.

The default answer in object oriented programming: create another layer of abstraction .

Meaning: you start with something like:

public interface Authentification {
  public void login();
  public void logout();
}

and then you create different classes that implement this interface.

And then, at runtime, you have one single class/method that knows which implementation to use for a user - depending if you need Google/Facebook/whatever. This method does say Authentification as return type.

And all your other code only deals with objects of which they now: they implement that interface.

I would propose that you take a look at Firebase Authentication which supports multiple authentication providers like Facebook and Google.

Firebase will take care of login and of logout of the selected authentication provider and you do not have to develop the login and logout procedures for every new authentication provider.

You will find the documentation here:

https://firebase.google.com/docs/auth/android/start/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM