Sign out from Google Account using Firebase Authentication

I have an app where I’m signing in using Google account using Firebase authentication. Here is the code of signing in.

 List<AuthUI.IdpConfig> providers;
 providers= Arrays.asList(
                new AuthUI.IdpConfig.EmailBuilder().build(),
                new AuthUI.IdpConfig.GoogleBuilder().build()

        );
 private void showSignInOptions() {
        startActivityForResult(
                AuthUI.getInstance()
                        .createSignInIntentBuilder()
                        .setAvailableProviders(providers)
                        .build(),MY_REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==MY_REQUEST_CODE)
        {
            IdpResponse response=IdpResponse.fromResultIntent(data);
            if(resultCode==RESULT_OK)
            {

                final FirebaseUser firebaseUser= FirebaseAuth.getInstance().getCurrentUser();
                assert firebaseUser != null;
                user_information.orderByKey()
                        .equalTo(firebaseUser.getUid())
                        .addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                                if(dataSnapshot.getValue()==null)
                                {
                                    if(!dataSnapshot.child(firebaseUser.getUid()).exists()){
                                        Common.loggedUser=new User(firebaseUser.getUid(),firebaseUser.getEmail());
                                        user_information.child(Common.loggedUser.getUid())
                                                .setValue(Common.loggedUser);
                                    }
                                }
                                else {
                                    Common.loggedUser=dataSnapshot.child(firebaseUser.getUid()).getValue(User.class);
                                }
                                Paper.book().write(Common.USER_UID_SAVE_KEY,Common.loggedUser.getUid());

                                updateToken(firebaseUser);
                                setUpUI();


                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError error) {

                            }
                        });

            }
        }
    }

The code works fine and after logging in it sends me to NavigationDrawerActivity. In the Navigation I have an option Sign Out. Now what I want to do is, when I click on Sign out, it should sign me out of my Google Account and next time when I come to login activity, it should again show me the option sign in with google and when I click, it should ask to select Google account as it did when the app was first installed.

I tried the following code which I got from answers of stackoverflow but it doesn’t work. It redirects me to login activity but doesn’t show any options and I’m logged in directly.

Here is the code, I tried.

 GoogleSignInClient googleSignInClient;
                  GoogleSignInOptions gso=new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                          .requestIdToken(getString(R.string.default_web_client_id))
                          .requestEmail()
                          .build();
                  googleSignInClient=GoogleSignIn.getClient(getBaseContext(),gso);
                  googleSignInClient.signOut().addOnCompleteListener(NavigateActivity.this,
                          new OnCompleteListener<Void>() {
                              @Override
                              public void onComplete(@NonNull Task<Void> task) {
                                  FirebaseAuth.getInstance().signOut();
                                  Intent setupintent=new Intent(getBaseContext(),login.class);
                                  setupintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                  startActivity(setupintent);
                                  finish();
                              }
                          });

Please guide me how to show the google builder again when I log out from google account.

Answer

When you use AuthUI you don’t need to sign out with GoogleSignInClient. You can do it from directly AuthUI like:

AuthUI.getInstance()
      .signOut(context)
      .addOnSuccessListener {  // start login activity }

Leave a Reply

Your email address will not be published. Required fields are marked *