如何根据 Firebase 信息进行页面重定向

huangapple go评论63阅读模式
英文:

How to redirect to different pages based on Firebase Information

问题

[Firebase结构][1]

  [1]: https://i.stack.imgur.com/BjS5L.png

对于我的项目我希望允许用户在使用Firebase的电子邮件和密码登录方法登录后根据其身份例如买家卖家允许他们进入相应的主页我不确定如何完成这个过程

以下是我尝试过的方法但无论是买家还是卖家都会被导向到卖家主页

```java
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,"错误!" +task.getException().getMessage(),Toast.LENGTH_SHORT).show();
        }
    }
});
英文:

Firebase Structure

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.

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

 fAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener&lt;AuthResult&gt;() {
@Override
public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
if (task.isSuccessful()) {
db = FirebaseDatabase.getInstance().getReference(&quot;users&quot;);
db.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren() ) {
String userType = ds.child(&quot;type&quot;).getValue(String.class);
if (userType.equals(&quot;Buyer&quot;)) {
startActivity(new Intent(MainActivity.this,BuyerHomepage.class));
}
else if(userType.equals(&quot;Seller&quot;)) {
startActivity(new Intent(MainActivity.this,SellerHomepage.class));
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
else {
Toast.makeText(MainActivity.this,&quot;Error !&quot; +task.getException().getMessage(),Toast.LENGTH_SHORT).show();
}
}
});

答案1

得分: 0

@Danial 检查一下这段代码

  
    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) {
    
        }
    });
   
英文:

@Danial check this code

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

huangapple
  • 本文由 发表于 2020年10月21日 18:13:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64461357.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定