zb22
zb22

Reputation: 3231

Firebase sign out after few tries

I'm using Firebase and I added the signOut() method in onBackPressed() to return to the Log-in activity. But when I press "back" in order to sign out, I return to log-in activity and onAuthStateChangedtakes me back the next acitivity after signing in. Somehow I still logged in.

It works after the second or third time when I press on the back button. it's really weird, I hope you can help me.. here are some snippets of my code:

This is the onBackPressed() in the activity after signing in:

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();

         FirebaseAuth.getInstance().signOut();
         finish();

    }
}

This is the AuthStateListener() in the log-in activity:

  mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                curr_user = user;

                Toast.makeText(LoginActivity.this, "yes", Toast.LENGTH_SHORT).show();
                if(curr_user.getDisplayName() == null)
                    autoSignIn("email");
                else
                    autoSignIn("google");

            user.getUid());
            } else {
                Toast.makeText(LoginActivity.this, "out", Toast.LENGTH_SHORT).show();                 
            }           
        }
    };

And this is the autoSignIn() method which start next activity:

public void autoSignIn(String accType) {

    if(accType.equals("email")) {

        mDatabaseReference.child("users").orderByKey().equalTo(curr_user.getUid());

        Query myTopPostsQuery = mDatabaseReference.child("users").orderByKey().equalTo(curr_user.getUid());
        myTopPostsQuery.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                                    editor = pref.edit();
                                    User user = snapshot.getValue(User.class);
                                    editor.putString("DisplayName", user.username);
                                    editor.commit();
                                    intent = new Intent(getApplicationContext(), MainActivity.class);
                                    intent.putExtra("userID", curr_user.getUid());
                                    startActivity(intent);
                }

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }
    else {
        Toast.makeText(this,"google", Toast.LENGTH_SHORT).show();

        txtWelcome.setText("Hello " + curr_user.getDisplayName());
        intent = new Intent(getApplicationContext(), MainActivity.class);
        intent.putExtra("userID", curr_user.getUid());
        editor = pref.edit();
        editor.putString("DisplayName", curr_user.getDisplayName());
        editor.commit();
        startActivity(intent);

    }
}

Upvotes: 3

Views: 382

Answers (2)

zb22
zb22

Reputation: 3231

figured this out eventually, Somehow after FirebaseAuth.getInstance().signOut();and finish() it runs the code inside onAuthStateChanged when user != null. I removed calling to the method autoSignIn() and it worked.

Upvotes: 1

Nishanth Sreedhara
Nishanth Sreedhara

Reputation: 1286

Call super.onBackPressed() after signing out.

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        FirebaseAuth.getInstance().signOut();
        super.onBackPressed();
        finish();
    }
}

Upvotes: 0

Related Questions