英文:
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();
}
}
});
英文:
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<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();
}
}
});
答案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("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) {
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论