简体   繁体   中英

How to logout of Google Sign-In?

How do I logout out of Google Sign In?

I know that I would be able to call mGoogleSignInClient.signOut() , but I create the mGoogleSignInClient in my login activity. How could I access it in my settings activity (where the logout happens)?

val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestIdToken("id_token")
        .requestEmail()
        .build()

googleSignInClient = GoogleSignIn.getClient(this, gso)

In my settings activity, where I have the logout button, I want to be able to call some static method relating to the Google Sign-In SDK and logout.

Do I really need to repeat the steps above (which I implemented in my login activity) into my settings activity (where the logout button is)?

You can use this for logout click

Auth.GoogleSignInApi.signOut(googleApiClient).setResultCallback(new ResultCallback<Status>() {
   @Override
   public void onResult(@NonNull Status status) {
      //set your action after log out
   }
});

According to the docs , using the signInClient:

mGoogleSignInClient.signOut()
            .addOnCompleteListener(this, new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    // ...
                }
            });

I guess these answers (@arjun, @Till) don't really answer the question

Both of them assume the instance of GoogleSignInClient is present

But @rgoncalv asked: "Do I really need to repeat the steps above?"

This means: does he/she need to create the same instance of GoogleSignInClient (with the same options) to sign out as it was created in the login activity?

BTW I'm interested the same

As pointed out in this answer , in your login activity you can pass to GoogleSignIn.getClient() the application context instead of the activity context.

So in your settings activity you can simply do the following:

GoogleSignInClient googleSignInClient = GoogleSignIn.getClient(getApplicationContext(), GoogleSignInOptions.DEFAULT_SIGN_IN);
signOut(googleSignInClient);
revokeAccess(googleSignInClient);

The definitions of signOut() and revokeAccess() can be found here .

Its a bit tricky,

  • I used sharedpreferences to create a flag if user is logged in or not.
  • When I set the flag to logout, In my code I navigate back to login
    activity
  • On login activity -> onStart(), if sharedpreference flag is logged
    out but GoogleSignInAccount is not null , do the signout and show the login UI.

Note, for new Login the logout wont work since user is not logged in yet.

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