我正在Firebase上创建投票应用程序。我有3种类型的用户。到目前为止,在使用以下代码MY Users so far登录全部用户后,我可以成功将2种用户(学生,教师)重定向到他们各自的活动 但是现在我必须添加另一个用户(ADMIN),并且像其他用户一样,在登录后,管理员也应重定向到他们自己的特定活动。我对如何修改第三位用户的代码感到困惑。
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(FirebaseAuth firebaseAuth) {
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if (mAuth.getCurrentUser() != null) {
String uid = mAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
uidRef = rootRef.child("STUDENTS").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
//start students activity
startActivity(new Intent(MainActivity.this, student.class));
} else {
//start teachers activity
startActivity(new Intent(MainActivity.this, teacher.class));
}
}
//
@Override
public void onCancelled(DatabaseError databaseError)
{
}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
}
else{
Log.d("TAG", "firebaseUser is null");
}
}
};
答案 0 :(得分:1)
仅使用if (dataSnapshot.exists())
不能解决您的3种类型的用户问题。假设第三个用户的类型为3
,则需要更改数据库结构。因此,您的新数据库架构应如下所示:
Firebase-root
|
--- users
|
--- uidOne
| |
| --- name: "Ed"
| |
| --- type: 1
|
--- uidTwo
| |
| --- name: "Tyff"
| |
| --- type: 2
|
--- uidOne
|
--- name: "Admin"
|
--- type: 3
现在您应该在uid
节点上添加一个侦听器,并像这样检查用户的类型:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("users").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.child("Type").getValue(Long.class) == 1) {
startActivity(new Intent(MainActivity.this, student.class));
} else if (dataSnapshot.child("TYPE").getValue(Long.class) == 2) {
startActivity(new Intent(MainActivity.this, teacher.class));
} else if (dataSnapshot.child("TYPE").getValue(Long.class) == 3) {
startActivity(new Intent(MainActivity.this, admin.class));
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage());
}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
答案 1 :(得分:0)
您需要另一个数据结构,分别为users
和roles/admin
,roles/teacher
,roles/student
,然后检查哪个节点具有密钥(也与安全规则兼容)。
有了它,您只需删除else
分支并查询所有三个节点即可。
答案 2 :(得分:0)
这是我使用 Kotlin 的最终代码,它运行良好。
mAuth.signInWithEmailAndPassword(Email, Password).addOnCompleteListener { task ->
if (task.isSuccessful) {
val uid = FirebaseAuth.getInstance().currentUser!!.uid
val rootRef = FirebaseDatabase.getInstance().reference
val uidRef = rootRef.child("Users").child(uid)
val spin: String = userchioce.selectedItem.toString()
val valueEventListener: ValueEventListener = object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
when {
dataSnapshot.child("users").getValue(String::class.java) == "Admin" -> {
if (spin.equals("Admin")){
val intent = Intent(this@Login, Admin::class.java)
startActivity(intent)}
else{
Toast.makeText(this@Login, "Select Your User-Type", Toast.LENGTH_SHORT)
.show()
}
}
dataSnapshot.child("users").getValue(String::class.java) == "Lecturer" -> {
if (spin.equals("Lecturer")) {
val intent = Intent(this@Login, Lecturer::class.java)
startActivity(intent)
}else{
Toast.makeText(this@Login, "Select Your User-Type", Toast.LENGTH_SHORT)
.show()
}
}
dataSnapshot.child("users").getValue(String::class.java) == "Parent" -> {
if (spin.equals("Parent")) {
val intent = Intent(this@Login, Parent::class.java)
startActivity(intent)
}else{
Toast.makeText(this@Login, "Select Your User-Type", Toast.LENGTH_SHORT)
.show()
}
}
dataSnapshot.child("users").getValue(String::class.java) == "Student" -> {
if (spin.equals("Student")) {
val intent = Intent(this@Login, Student::class.java)
startActivity(intent)
}else{
Toast.makeText(this@Login, "Select Your User-Type", Toast.LENGTH_SHORT)
.show()
}
}
}
}
override fun onCancelled(databaseError: DatabaseError) {
Log.d(TAG, databaseError.message)
}
}
uidRef.addListenerForSingleValueEvent(valueEventListener)
progressDialog.dismiss()
}