为两个不同的用户提供不同的活动

时间:2019-01-28 16:50:29

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

我正在尝试开发一个可以让医生和患者登录的应用程序。 我使用Firebase进行身份验证,当他们登录后,我想为Doctor和Patient提供不同的布局。

我存储在注册类型(1-病人,2-博士)在实时数据库与他们的UID作为病人沿着/医生报。而且我尝试获取数据库的DataSnapshot并检查执行的Signup类型并提供基于此的活动。注册部分进行得很好,但是当我尝试登录时,它失败了,并带我回到Main_Activity。

我的数据库看起来是这样的:

database structure

参考:How to separate two different user in firebase android app?

firebaseAuth.signInWithEmailAndPassword(email,pass)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                  progressDialog.dismiss();
                  if (task.isSuccessful()) {
                      FirebaseUser user = firebaseAuth.getCurrentUser();
                      DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child(user.getUid()).child("type");
                      ref.addListenerForSingleValueEvent(new ValueEventListener() {
                          @Override
                          public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                              String value = dataSnapshot.getValue(String.class);
                              assert value != null;
                              if (Integer.parseInt(value) == 1) {
                                  startActivity(new Intent(Login.this, HomepagePatient.class));
                                  Toast.makeText(Login.this, "You're Logged in as Patient", Toast.LENGTH_SHORT).show();
                                  finish();
                              } else if (Integer.parseInt(value) == 2) {
                                  startActivity(new Intent(Login.this, HomepageDoctor.class));
                                  Toast.makeText(Login.this, "You're Logged in as Doctor", Toast.LENGTH_SHORT).show();
                                  finish();
                              }
                          }
                          @Override
                          public void onCancelled(@NonNull DatabaseError databaseError) {
                          }
                      });
                  }
                  else {
                      Toast.makeText(Login.this, "Failed, please try again", Toast.LENGTH_SHORT).show();
                  }
                }
            });

0 个答案:

没有答案
相关问题