如何将多种类型的用户重定向到他们各自的活动? [英] How to redirect multiple types of users to their respective Activities?

查看:19
本文介绍了如何将多种类型的用户重定向到他们各自的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating a voting app on Firebase. I have 3 types of users. So far i can successfully redirect 2 kinds of users (STUDENTS, TEACHERS) to their respective activities after they login all with the code below, MY Users so far But now i have to add another user (ADMIN) and like other users, admin too should be redirected to their own specific activity after login. I am confused as to how to modify my code for a third user.

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,你的数据库结构需要改变.因此,您的新数据库架构应如下所示:

Using onlye if (dataSnapshot.exists()) will not solve your 3 types of user problem. Assuming that the type of the third user is 3, a change in your database structure is needed. So your new database schema should look like this:

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

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

Now you shoud add a listener on the uid node and check the type of the user like this:

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);

这篇关于如何将多种类型的用户重定向到他们各自的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆