如何让不同类型的经过身份验证/验证的用户有意参与不同的活动?

时间:2018-02-20 12:08:20

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

我有两种类型的用户。我已经编写了电子邮件和电子邮件密码验证在他们两个。他们的个人资料数据已存储在Firebase数据库中。

在我的Launcher活动中,这是我的Welcome_page,它有一个带有2个按钮的应用徽标,可以注册为不同的用户。

现在,当用户注册并填写必要的字段时,他们可以提交该字段,并且会出现一个祝酒词,检查他们的电子邮件是否有验证链接。

因此,当我使用模拟器检查我的电子邮件并返回我的应用程序时,我被重定向到我的欢迎页面。所以我做了一些编码来检查用户是否经过验证。如果他们被验证,那么它将意图到另一个活动,如果没有,那么注册按钮将消失,并且Toast将显示“处理验证”。

我的问题是如何让userType1意图使用UserType1.class而userType2意图使用UserType2.class

我尝试编码以测试userType的ID是否使用addListenerForSingleValueEvent()

存储在数据库中特定的userType节点中 到目前为止,

代码如下所示:

public class Welcome_Page extends AppCompatActivity {

Button btnUserType1, btnUserType2;
private FirebaseAuth mAuth;

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRefUserType1 = database.getReference();
DatabaseReference myRefUserType2 = database.getReference();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome__page);

    btnUserType1 = (Button) findViewById(R.id.SignUpUserType1);
    btnUserType2 = (Button) findViewById(R.id.SignUpUserType2);

    mAuth = FirebaseAuth.getInstance();



   if (mAuth.getCurrentUser() != null) {
       final String uId = mAuth.getCurrentUser().getUid();
       myRefUserType1 = myRefUserType1.child("users").child("UserType1");
       myRefUserType2 = myRefUserType2.child("users").child("UserType2");

        if (mAuth.getCurrentUser().isEmailVerified()) {
            myRefUserType1.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                   for(DataSnapshot ds : dataSnapshot.getChildren()){
                       if(ds.child(uId).exists()){
                           Intent intent = new Intent(Welcome_Page.this, UserType1.class);
                           startActivity(intent);
                       }
                   }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

            myRefUserType2.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for(DataSnapshot ds : dataSnapshot.getChildren()) {
                        if (ds.child(uId).exists()) {
                            Intent intent = new Intent(Welcome_Page.this, UserType2.class);
                            startActivity(intent);
                        }
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }
        else {
            Toast.makeText(getApplicationContext(), "Processing verification...", Toast.LENGTH_SHORT).show();
        }
        btnUserType1.setVisibility(View.GONE);
        btnUserType2.setVisibility(View.GONE);
        mAuth.getCurrentUser().reload();
        //finish();
    }
    //mAuth.signOut(). 
        btnUserType1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                    Intent i = new Intent(Welcome_Page.this, SignUpUserType1.class);
                    startActivity(i);
                }
        });



        btnUserType2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(Welcome_Page.this, SignUpUserType2.class);
                startActivity(i);
            }
        });
    }

}

数据库:

{
 users:
 {
   userType1:
   {
     userType1_ID:
     {
       name     :  blach
       email    :  blach@gmail.com
       pass     :  123456
       user_Id  :  STwjhdaskjdfsadfi
     }
   }
   userType2:
   {
     userType2_ID:
      {
        name     :  asdfgh
        email    :  asdfgh@gmail.com
        pass     :  789012
        user_Id  :  KDIWOfhjdshfkjdffi
      }
   }
 }
}

但是当我运行应用程序时,屏幕闪烁白色,然后它立即关闭。甚至没有消息说“App已停止”。 Logcat中没有任何内容指示要修复的代码行。在我的应用程序停止后,模拟器还会以某种方式打开手机设置。

在为intent添加addListenerForSingleValueEvent()之前,一切正常。虽然用户当时会被重定向到同一个意图类。

有人可以告诉我如何让他们正确的意图。

1 个答案:

答案 0 :(得分:0)

您需要在数据库中设置2个不同的部分,userType1userType2。您的数据库应如下所示:

Firebase-root
     |
     --- userType1
     |     |
     |     --- userId1@email,com
     |     |       |
     |     |       --- //details
     |     |
     |     --- userId2@email,com
     |             |
     |             --- //details
     |
     --- userType2
           |
           --- userId3@email,com
           |       |
           |       --- //details
           |
           --- userId4@email,com
                   |
                   --- //details

正如您可能看到的,用户之间的唯一标识符是email address。在登录时,验证每个部分中的用户是否存在。为此,请附加listerner并在exists()对象上使用DataSnapshot方法。如果用户属于userType1类型,则显示Toast并将其重定向到actvity,如果类型为userType2,则显示另一个Toast并将其重定向到另一个活动。

希望它有所帮助。

相关问题