How to separate two different user in firebase android app?

时间:2017-04-09 23:11:53

标签: android firebase firebase-realtime-database firebase-authentication

I have an application with two different types of users one as Teacher and the Second a normal user. if a normal member logs in, he will go to normal_memberActivity and if he's a Teacher member, he'll go to Teacher_memberActivity. How do I do this in loginActivity?

My Firebase structure:

1

this is database firebase rules

 { 
   "rules": {
  ".read": true,

  ".write":true}

}

3 个答案:

答案 0 :(得分:0)

I suggest you have this kind of structure in your database

users
---details...
---type

The type key would now contain whether your user is normal or a teacher. Now using FirebaseAuthentication which I think you already have integrated in your app, you could use:

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

to get the currently logged in user. With that, you can now use:

String uid = user.getUid();

in order to get the UID of the currently logged in user which I think you have used as the key for your users.

Now with the UID ready, you could use a Firebase query to get the type of that specific user and go to the specific Activity based on their type.

Query userQuery = FirebaseDatabase.getInstance().getReference()
                     .child("users").orderByKey().equalTo(uid);
userQuery.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String type = dataSnapshot.child("type").getValue().toString();
        if(type.equals("Teacher"){
            //Go to TeacherMemberActivity
        } else {
            //Go to NormalMemberActivity
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

答案 1 :(得分:0)

You can verify what group a user belongs(teacher or user) by attaching an event listener inside the FirebaseAuth.AuthStateListener and redirecting the user to the appropriate activity as follows

private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference ref;

    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                ref = FirebaseDatabase.getInstance().getReference().child("hire-teachers").child("teachers");

                ref.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                            if(FirebaseAuth.getInstance().getCurrentUser().getUid().equals(snapshot.getKey())){
                                startActivity(new Intent(SignInActivity.this, Teacher_memberActivity.class));
                            }
                        }
                        startActivity(new Intent(SignInActivity.this, Normal_memberActivity.class));
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });

            } else {
                // User is signed out
            }
            // ...
        }
    };  

In the above implementation of the Firebase API, I am checking to see if the user is a teacher. if true then start the Teacher_memberActivity if not, start the Normal_memberActivity.

答案 2 :(得分:0)

这是新代码:

private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference ref;

mAuthListener = new FirebaseAuth.AuthStateListener() {
    @Override
    public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
        FirebaseUser user = firebaseAuth.getCurrentUser();
        if (user != null) {
            ref = FirebaseDatabase.getInstance().getReference().child("hire-teachers").child("teachers");

            ref.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    Boolean boolean = false;
                    for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                        if(FirebaseAuth.getInstance().getCurrentUser().getUid().equals(snapshot.getKey())){
                           boolean = true;
                        }
                    }
                    if(boolean){
                        startActivity(new Intent(SignInActivity.this, Teacher_memberActivity.class));
                    }else{
                        startActivity(new Intent(SignInActivity.this, Normal_memberActivity.class));
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

        } else {
            // User is signed out
        }
        // ...
    }
};