0

I am checking if the current user is logged into my app and has never once signed out. If it is true, and upon launching my app, it will open Activity A/B, depending on the value of userDepartment stored in database.

My codes are like this:

        firebaseAuth = FirebaseAuth.getInstance();
    FirebaseUser user = firebaseAuth.getCurrentUser();

    //user has logged in and never once logged out. auto bring to the specific dashboard page based on account type.
    if (user != null) {
        finish();
        final String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("userDepartment").child(uid);
        if (databaseReference.equals("Administrator")) {
            startActivity(new Intent(MainActivity.this, SecondActivity.class));
        } else {
            startActivity(new Intent(MainActivity.this, LocalUserDashboard.class));
        }
    }

The logic is not working as when I signed in as Administrator, close the app but DID NOT sign out, the expected outcome is the Administrator user will be brought to SecondActivity class. However, it opened up the LocalUserDashboard class instead. Then, if I login as Local User, close the app and did not sign out, it does bring me to the LocalUserDashboard class, just as intended.

I am not entirely sure if my codes are right as I did it while depending on various sources both in SO as well as the official documents and kinda just mixing everything up.

Please help me. Thank you.

Below is my database structure:

enter image description here

F4y5
  • 43
  • 10
  • you are comparing object (DatabaseReference) with String ("Administrator"), so it always returns false. Try to cast into string and compare with equalIgnoreCase. – Arpit bandil Jan 21 '19 at 06:51
  • Check also [this](https://stackoverflow.com/questions/50885891/one-time-login-in-app-firebaseauth) out. – Alex Mamo Jan 21 '19 at 09:37
  • @AlexMamo The one you linked as duplicate solved my problem, with a little tweaking with Chetan Shelake's answers. Thank you sm! – F4y5 Jan 22 '19 at 02:16

1 Answers1

1

You are directly comparing with databaseReference, do the request first to get data from firebase with respected userId

databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if(dataSnapshot!=null)
        {
          String userDepartment = dataSnapshot.child("userDepartment").getValue());
          //Now check with this userDepartment
          if (userDepartment .equals("Administrator")) {
             startActivity(new Intent(MainActivity.this, SecondActivity.class));
          } else {
            startActivity(new Intent(MainActivity.this, LocalUserDashboard.class));
        }
    }

}
Chetan Shelake
  • 656
  • 1
  • 6
  • 14
  • Thank you for your response! I have a red underline under: String userDepartment = dataSnapshot.child("userDepartment").getValue()); Do I have to cast the dataSnapshot... to String? – F4y5 Jan 21 '19 at 07:21
  • Also, is everything else before addListenerForSingleValueEvent(new ValueEventListener() remain as what it is? Because if it is, I am getting NPE "Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference" :/ – F4y5 Jan 21 '19 at 07:28
  • 1
    You should learn first how to read data from firebase so you will get all knowledge about firebase https://firebase.google.com/docs/database/admin/retrieve-data follow this document. – Chetan Shelake Jan 21 '19 at 07:52
  • Of course, thank you for your help thus far – F4y5 Jan 21 '19 at 08:28
  • 1
    Hey, I have used your answers with lil changes here and there and gotten some from Alex Momo's past answers too so now it worked! Thank you for your guidance!! – F4y5 Jan 22 '19 at 02:18
  • Nice.. if you want more help about firebase i will help you. – Chetan Shelake Jan 22 '19 at 05:17