Ad

Sign Out Button Doesn't Works. Android - Firebase

Logout doesn't work. Nothing happens when you click. Does anyone see a mistake?

The code is too long so - here is my MainActivity (https://www.paste.org/90991):

    public String userEmail, userName;
    private FirebaseAuth firebaseAuth;
    private FirebaseFirestore rootRef;
    private FirebaseAuth.AuthStateListener authStateListener;
    private GoogleApiClient googleApiClient;

    public String getUserEmail() {return userEmail;}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

        GoogleSignInAccount googleSignInAccount = GoogleSignIn.getLastSignedInAccount(this);
        if(googleSignInAccount != null){
            userEmail = googleSignInAccount.getEmail();
            userName = googleSignInAccount.getDisplayName();
            Toast.makeText(this, "Witaj " + userName, Toast.LENGTH_LONG).show();
        }

        googleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Auth.GOOGLE_SIGN_IN_API)
                .build();

        firebaseAuth = FirebaseAuth.getInstance();
        rootRef = FirebaseFirestore.getInstance();

        authStateListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
                if(firebaseUser == null){
                    Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                    startActivity(intent);
                }
            }
        };

    private void signOut(){
        Map<String> map = new HashMap<>();
        map.put("tokenId", FieldValue.delete());

        rootRef.collection("users").document(userEmail).update(map).addOnSuccessListener(new OnSuccessListener<Void>(){
            @Override
            public void onSuccess(Void aVoid){
                firebaseAuth.signOut();

                if(googleApiClient.isConnected()){
                    Auth.GoogleSignInApi.signOut(googleApiClient);
                }
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        googleApiClient.connect();
        firebaseAuth.addAuthStateListener(authStateListener);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(googleApiClient.isConnected()){
            googleApiClient.disconnect();
        }
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.sign_out_button:
                signOut();
                return true;
            case R.id.action_exit:
                System.exit(0);
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

here is loginActivity (It works): https://www.paste.org/90992

Thank you in advance for all the tips!

EDIT: I added the most important parts of the code here, but I could not add the whole.

Ad

Answer

You are getting this behaviour because your database is empty. When using this line of code:

rootRef.collection("users").document(userEmail).update(map).addOnSuccessListener(/* ... */)

You are signing out, only when the token is successfully updated. Because the token is never updated as you don't have any data in your database, the onComplete() method is never triggered. To solve this, use the following code if don't need the token:

private void signOut(){
    firebaseAuth.signOut();

    if(googleApiClient.isConnected()){
        Auth.GoogleSignInApi.signOut(googleApiClient);
    }
}

Or uninstall the app and sing-in again to generate the user in your database. This will solve the problem for sure.

Ad
source: stackoverflow.com
Ad