如何根据 Firebase 信息重定向到不同的页面 [英] How to redirect to different pages based on Firebase Information

查看:35
本文介绍了如何根据 Firebase 信息重定向到不同的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firebase 结构

对于我的项目,我希望允许用户在单击带有 firebase 使用电子邮件和密码方法登录的按钮后,根据他们的身份(例如:买方、卖方)登录到各自的主页.我不确定如何完成它.

For my project, I would like to allow users to login to respective homepages based on their identity (eg: Buyer,Seller) after clicking a button with the firebase sign in with email and password method. I am unsure about the way to get it done.

这是我尝试过的方法,我被引导到针对买家和卖家的 SellerHomepage.

Here's what I tried and I was directed to SellerHomepage for both Buyers and Sellers.

 fAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
              @Override
              public void onComplete(@NonNull Task<AuthResult> task) {
                  if (task.isSuccessful()) {
                      db = FirebaseDatabase.getInstance().getReference("users");
                      db.addValueEventListener(new ValueEventListener() {
                          @Override
                          public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                              for (DataSnapshot ds : dataSnapshot.getChildren() ) {
                                  String userType = ds.child("type").getValue(String.class);
                                  if (userType.equals("Buyer")) {
                                      startActivity(new Intent(MainActivity.this,BuyerHomepage.class));
                                  }
                                  else if(userType.equals("Seller")) {
                                      startActivity(new Intent(MainActivity.this,SellerHomepage.class));
                                  }
                              }
                          }

                          @Override
                          public void onCancelled(@NonNull DatabaseError databaseError) {

                          }
                      });
                  }
                  else {
                      Toast.makeText(MainActivity.this,"Error !" +task.getException().getMessage(),Toast.LENGTH_SHORT).show();
                  }
              }
          });

推荐答案

@Danial 检查此代码

@Danial check this code

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference("users");
rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        for (DataSnapshot dataSnapshot:snapshot.getChildren()){
            String mail=dataSnapshot.child("type").getValue().toString();
             if (userType.equals("Buyer")) {
          startActivity(new Intent(MainActivity.this,BuyerHomepage.class));
                              }
                            if(userType.equals("Seller")) {
           startActivity(new Intent(MainActivity.this,SellerHomepage.class));
                              }
        }
  
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {

    }
});

这篇关于如何根据 Firebase 信息重定向到不同的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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