如何访问Firebase数据库中的特定角色?

时间:2017-04-18 18:59:06

标签: android firebase firebase-realtime-database

这是我的示例数据库结构:

{
  "2pwX0ehEbBg6N6Qow3GgOQpcZEs2" : {
    "role" : "Patient"
  },
  "FzbS0If8QXfau7iQ7Qbxnk4wIQy2" : {
    "role" : "Therapist"
  }
}

我正在构建一个Android应用程序,当用户登录到该应用程序时,我想根据他们的role为他们提供活动访问权限。我不知道我做错了什么,但我的应用程序崩溃了。

以下是我访问角色的代码:

private void getRole()
{

    final FirebaseUser user = firebaseAuth.getCurrentUser();
    String uid = user.getUid();
    Toast.makeText(LoginActivity.this,uid, Toast.LENGTH_LONG).show();

    final FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference dbRef = database.getReference("Roles").child(uid);
    DatabaseReference roleRef = dbRef.child("role");

    /*
    String uid= FirebaseAuth.getInstance().getCurrentUser().getUid();
    Toast.makeText(LoginActivity.this,uid, Toast.LENGTH_LONG).show();
    DatabaseReference userRef = FirebaseDatabase.getInstance().getReference().child("Roles").child(uid);
    */
    roleRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String Administrator = "Administrator";
            String Therapist = "Therapist";
            String Patient = "Patient";
            String role = "";
            role = (String) dataSnapshot.getValue();

            if(role.equals(Administrator))
            {
                Intent intent = new Intent(LoginActivity.this, AdminActivity.class);
                startActivity(intent);
                finish();
            }
            else if(role.equals(Patient))
            {
                Intent intent = new Intent(LoginActivity.this, SessionSelectActivity.class);
                startActivity(intent);
                finish();
            }
            ....

        @Override
        public void onCancelled(DatabaseError databaseError)
        {
            Toast.makeText(LoginActivity.this, databaseError.getCode(), Toast.LENGTH_LONG).show();
        }
    });
}

0 个答案:

没有答案