java - 如何将多种类型的用户重定向到各自的Activity?

标签 java android firebase android-studio firebase-authentication

我正在 Firebase 上创建一个投票应用程序。我有 3 种类型的用户。到目前为止,我可以成功地将 2 种用户(学生、教师)重定向到他们各自的 Activity ,在他们使用下面的代码登录后,MY Users so far 但现在我必须添加另一个用户 (ADMIN) 并且像其他用户一样,管理员也应该在登录后被重定向到他们自己的特定 Activity 。我对如何为第三个用户修改我的代码感到困惑。

mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(FirebaseAuth firebaseAuth) {
            FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
            if (mAuth.getCurrentUser() != null) {

                String uid = mAuth.getInstance().getCurrentUser().getUid();
                DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
                uidRef = rootRef.child("STUDENTS").child(uid);

                ValueEventListener valueEventListener = new ValueEventListener() {
                    @Override
                    public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
                        if (dataSnapshot.exists()) {
                            //start students activity
                            startActivity(new Intent(MainActivity.this, student.class));

                        } else {
                            //start teachers activity
                            startActivity(new Intent(MainActivity.this, teacher.class));
                        }
                    }

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


uidRef.addListenerForSingleValueEvent(valueEventListener);

            }
            else{
                Log.d("TAG", "firebaseUser is null");
            }


        }
    };

最佳答案

仅使用 if (dataSnapshot.exists()) 不会解决您的 3 种用户问题。假设第三个用户的类型是 3,则需要更改您的数据库结构。所以你的新数据库模式应该是这样的:

Firebase-root
    |
    --- users
          |
          --- uidOne
          |     |
          |     --- name: "Ed"
          |     |
          |     --- type: 1
          |
          --- uidTwo
          |     |
          |     --- name: "Tyff"
          |     |
          |     --- type: 2
          |
          --- uidOne
                |
                --- name: "Admin"
                |
                --- type: 3

现在您应该在 uid 节点上添加一个监听器并像这样检查用户的类型:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("users").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if(dataSnapshot.child("Type").getValue(Long.class) == 1) {
            startActivity(new Intent(MainActivity.this, student.class));
        } else if (dataSnapshot.child("TYPE").getValue(Long.class) == 2) {
            startActivity(new Intent(MainActivity.this, teacher.class));
        } else if (dataSnapshot.child("TYPE").getValue(Long.class) == 3) {
            startActivity(new Intent(MainActivity.this, admin.class));
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage());
    }
};
uidRef.addListenerForSingleValueEvent(valueEventListener);

关于java - 如何将多种类型的用户重定向到各自的Activity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58941173/

相关文章:

java - GridView 中列之间的图像间距

java - java中从数组平铺 map

java - 从可能有重复条目的数据库列中使用 AutoCompleteTextView

firebase - 将 Firebase 崩溃报告更新为 Firebase Crashlytics : still on old dashboard

firebase - 将 Cloud Run 上的 Golang 应用程序与 Firestore 连接时出现问题

java - 如何更改微调项目中文本的背景颜色?

java - 如何从 APK 文件恢复 Gradle 文件?

java - 如何使用 CameraKit 拍照?

java - 覆盖 getView() 的问题

Android Firebase 只更新部分字段