Error implementing FirebaseAuth AuthStateListener

0

I would like to get the following flow in my App:

  • The user registers, validates email and then initiates session
  • Once inside, if I delete the account from the Firebase console, redirect the user to MainActivity (because obviously he no longer has an account)
  • I have everything, except to verify if the user is null . I tried to do it but, the App closes and I get the error:

      

    java.lang.NullPointerException: Attempt to invoke interface method 'void com.google.firebase.auth.FirebaseAuth $ AuthStateListener.onAuthStateChanged (com.google.firebase.auth.FirebaseAuth)' on a null object reference

    Apart from this, so that the Toast appears, I must go to another Activity and then return so that the Listener is executed. Is not it supposed to work within the same Activity?

    The idea is always to check if the user who is in the App is valid or not (example, if it has been deleted or if the account has been deactivated).

    Code:

    import com.google.firebase.auth.FirebaseAuth;
    import com.google.firebase.auth.FirebaseUser;
    
    public class HomeActivity extends AppCompatActivity {
    
        private FirebaseAuth mAuth;
        private FirebaseAuth.AuthStateListener mAuthListener;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_home);
    
            mAuth = FirebaseAuth.getInstance();
    
            mAuth.addAuthStateListener(new FirebaseAuth.AuthStateListener(){
            @Override
            public void onAuthStateChanged (@NonNull FirebaseAuth firebaseAuth){
                if (mAuth.getCurrentUser() != null) {
                    Toast.makeText(HomeActivity.this, "User is not null", Toast.LENGTH_SHORT).show();
                } else {
                Toast.makeText(HomeActivity.this, "User is null", Toast.LENGTH_SHORT).show();
            }
        }
    });
    
        }
    
        @Override
        protected void onStart() {
            super.onStart();
    
            mAuth.addAuthStateListener(mAuthListener);
        }
    
        @Override
        protected void onStop() {
            super.onStop();
    
            if (mAuthListener != null) {
                mAuth.removeAuthStateListener(mAuthListener);
            }
        }
    
    }
    

    I'm using:

  • Android Studio 3.1.4
  • implementation 'com.google.firebase:firebase-auth:16.0.3'
  • Any idea why it does not work for me?

        
    asked by Robert Gomez 15.08.2018 в 18:34
    source

    1 answer

    1

    What you can do to check if the user in question is or not logged on by entering onCreate() is the following

    mAuthListener = new FirebaseAuth.AuthStateListener() { 
    @Override 
    public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { 
    FirebaseUser user = firebaseAuth.getCurrentUser(); 
    if (user != null) { 
    checkUserExists();
    } else { 
    
          // El usuario es null
          } 
       } 
    };
    

    Method to check if the user exists or not of the database

    private void checkUserExists(){
    
       //y aca vemos si no es null , chequeamos que exista en la base de datos
                    mDatabase.child(userid).addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            if (dataSnapshot.exists())
    
                            //codigo si existe usuario
                              else
                            //Llevar a otra Activity u otra opcion
    
    
                        }
    
    
                    }
    
                    @Override
                    public void onCancelled (DatabaseError databaseError){
                        System.out.println("The read failed: " + databaseError.getCode());
                    }
                });
    
    
     }
    

    Remember to attack the auth listener in onStart()

    public void onStart() {
            super.onStart(); 
    
             mAuth.addAuthStateListener(mAuthListener);
    
        }
    
        
    answered by 15.08.2018 / 20:08
    source