-1
private void LoginUser(final String email,final String password) {
    mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(task -> {
        if (task.isSuccessful()) {
            String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
            DatabaseReference mRootRef = FirebaseDatabase.getInstance("https://fyp2-darren-default-rtdb.asia-southeast1.firebasedatabase.app").getReference();
            mRootRef.child("Users").child(uid).child("User Information").addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    if(dataSnapshot.child("userType").getValue(String.class).equals("admin")) {
                        Toast.makeText(LogInActivity.this, "LogIn successful", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(LogInActivity.this, AdminMainPageActivity.class);
                        startActivity(new Intent(getApplicationContext(), AdminMainPageActivity.class));
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);

                    } else if (dataSnapshot.child("userType").getValue(String.class).equals("users")) {
                        Toast.makeText(LogInActivity.this, "LogIn successful", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(LogInActivity.this, HomePage.class);
                        startActivity(new Intent(getApplicationContext(), AdminMainPageActivity.class));
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                        // startActivity(new Intent(getApplicationContext(), PatientMainPageActivity.class));
                    }

                }

LogInActivity$2.onDataChange(LogInActivity.java:86)

A few days ago this login still worked, but recently I tried to login with a new account and this happened.

Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61

1 Answers1

0

It looks like there is no node for the UID or that node doesn't have a string property userType.

Your code should probably handle the absence of the node for the user with:

public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    if (!dataSnapshot.exists()) throw new IllegalStateException("User "+uid+" not found in database");
    ...
}

It could also be that the UID node exists, but doesn't have a userType. I recommend actually validating that a user node always has a userType in security rules to validate data.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • My firebase User --uid ---User Information -----name -----usertype – SUSHI Y Dec 29 '22 at 17:27
  • I'm not sure what you mean with that comment @SUSHIY. Did you try the change I proposed in my answer? Is there any error message? – Frank van Puffelen Dec 29 '22 at 17:38
  • if(dataSnapshot.child("userType").getValue(String.class).equals("admin")){ .... } else if (dataSnapshot.child("userType").getValue(String.class).equals("users")) { .... } This is my code, if i use your solution there is an error – SUSHI Y Dec 30 '22 at 07:49
  • First off: did my answer make sense? I.e. could it indeed be that no node exists for the current user? And does it make sense how that would then lead to the code in your question not working, and how the code in my answer work address that? – Frank van Puffelen Dec 30 '22 at 15:50