izadora
izadora

Reputation: 13

Why my app closes when the user logs out of the account?

When the user logs out the application closes.

The problem came when I changed the real-time rules from this

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

to this

{
  "rules": {
    ".read": "auth.uid != null",
    ".write": "auth.uid != null"
    }
}

LOG OUT BUTTON

        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FirebaseAuth.getInstance().signOut();
                Intent intent = new Intent(getActivity(), MainActivity.class);
                startActivity(intent);
                getActivity().finishAffinity();
            }
        });

Upvotes: 0

Views: 51

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599686

It sounds like your app crashes, in which case there will be an error message and stack trace written to the logcat output. That will tell you what the problem is, and where it occurs in your code.

That said, given that it happened when you changed the security, you most likely have an active listener to your database when you sign out. Since you have your rules set to ".read": "auth.uid != null", this means that the user must be authenticated to be able to read/listener for the data. So when they sign out, that condition is no longer met.

My guess is that you have an onCancelled in the listener, that is implemented as:

@Override
public void onCancelled(DatabaseError databaseError) {
    throw databaseError.toException();
}

So that exception is in that case what's causing your app to crash.

Your two main options for fixing this crash:

  1. Find what listeners you have active, and [remove them] before signing the user out. This is the clean solution, but it is definitely more work than...

  2. Don't throw an exception, but log the problem with:

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.e("Firebase", "Database listener cancelled", databaseError.toException());
    }
    

Upvotes: 1

Related Questions