Nhow
Nhow

Reputation: 3

How can I get child value with if & else condition (Firebase,SDK)

I am having trouble with my codes. What I want to do in my Login page is log as admin using email/password and when "role" is equal to admin (the other role is Guest). The admin page is MainActivity.class and the Guest page is Home.class. Can someone help me :)

enter image description here

And here is my code:

    private void onAuthSuccess(FirebaseUser user) {
    DatabaseReference ref = 
    FirebaseDatabase.getInstance().getReference().child("users");
    ValueEventListener valueEventListener = new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                String role = ds.getKey();

                DatabaseReference userKeyDatabase =  FirebaseDatabase.getInstance().getReference().child("users");
                ValueEventListener eventListener = new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot == null) {
                Toast.makeText(getApplicationContext(), "****NULLL****", Toast.LENGTH_LONG).show();

            } else if(dataSnapshot.child("role").getValue().equals("Admin")) {
                startActivity(new Intent(SignInActivity.this, MainActivity.class));
                finish();
            }else{
                startActivity(new Intent(SignInActivity.this, Home.class));
                finish();
            }
        }

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

        userKeyDatabase.addListenerForSingleValueEvent(eventListener);
    }}
            @Override
            public void onCancelled(DatabaseError databaseError) {}
        };
       ref.addListenerForSingleValueEvent(valueEventListener);
    }

I am not sure about my code as well :(

Upvotes: 0

Views: 527

Answers (1)

Swati
Swati

Reputation: 28522

To check role of users login you can do something like this

    FirebaseDatabase firebaseDatabase=FirebaseDatabase.getInstance();
                                DatabaseReference db=firebaseDatabase.getReference().child("users");

     db.orderByChild("email").equalTo(email).addListenerForSingleValueEvent(new ValueEventListener() {
                                @Override
                                public void onDataChange(@NonNull DataSnapshot dataSnapshot) 
                                        if(dataSnapshot.exists()){

                                            Log.d("user exits","usere hai");
                                           String value =dataSnapshot.child("role").getValue().toString();

                                           if(value.equals("Admin") && value != null){
 startActivity(new Intent(SignInActivity.this, MainActivity.class));
                finish();
                                        }else{
 startActivity(new Intent(SignInActivity.this, Home.class));
                finish();
                                        }

                                }

                                @Override
                                public void onCancelled(@NonNull DatabaseError databaseError) { }
                            });

Upvotes: 1

Related Questions