使用Firebase身份验证注册帐户时,我将电子邮件存储在2个类别的教师和学生中。我使用email
和password
将电子邮件添加到Firestore 2个不同类别的教师和学生。登录时,我想检查电子邮件属于哪个类别(教师或学生),该怎么办?我尝试了Firestore查询isequalsto,但没有区别。
还有其他可能的解决方法吗?
这是我的登录活动代码
package com.example.dell.step;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QuerySnapshot;
public class LoginActivity extends AppCompatActivity {
private Button mRegbtn;
private Button mloginbtn;
private EditText mEmail;
private EditText mPassword;
private Toolbar mToolbar;
private FirebaseAuth mAuth;
private ProgressDialog mLoginDialog;
private FirebaseFirestore firebaseFirestore;
private CollectionReference collectionReference_I;
private CollectionReference collectionReference_S;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mAuth = FirebaseAuth.getInstance();
//Progress Dialog
mLoginDialog = new ProgressDialog(this);
//FOr Toolbar
mToolbar = findViewById(R.id.login_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("Login Account");
mloginbtn = findViewById(R.id.login_btn);
mEmail = findViewById(R.id.login_email);
mPassword = findViewById(R.id.login_password);
mRegbtn = findViewById(R.id.login_reg_btn);
firebaseFirestore = FirebaseFirestore.getInstance();
mRegbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent regIntent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(regIntent);
finish();
}
});
mloginbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = mEmail.getText().toString();
String password = mPassword.getText().toString();
if (!TextUtils.isEmpty(email) && !TextUtils.isEmpty(password)) {
mLoginDialog.setTitle("Login User");
mLoginDialog.setMessage("Please wait while we login to your account :)");
//it stops cancel Dialog when user touch on screen
mLoginDialog.setCanceledOnTouchOutside(false);
mLoginDialog.show();
login_user(email, password);
}
}
});
}
private void login_user(final String email, final String password) {
collectionReference_I = firebaseFirestore.collection("Teacher");
collectionReference_S = firebaseFirestore.collection("Student");
collectionReference_I.whereEqualTo("teacher_email", email).get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot documentSnapshots) {
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
//Dismiss the progress Dialog
mLoginDialog.dismiss();
//user is registered
Toast.makeText(LoginActivity.this, "Welcome to Teacher Account", Toast.LENGTH_SHORT).show();
Intent mainIntent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(mainIntent);
finish();
} else {
mLoginDialog.hide();
//if user is not registered
Toast.makeText(LoginActivity.this, "Cannot Log in Please check the form and try again!", Toast.LENGTH_LONG).show();
}
}
});
}
});
collectionReference_S.whereEqualTo("Student_email", email).get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot documentSnapshots) {
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
//Dismiss the progress Dialog
mLoginDialog.dismiss();
//user is registered
Toast.makeText(LoginActivity.this, "Welcome to Student Account", Toast.LENGTH_SHORT).show();
Intent mainIntent = new Intent(LoginActivity.this, StudentMain.class);
startActivity(mainIntent);
finish();
} else {
mLoginDialog.hide();
//if user is not registered
Toast.makeText(LoginActivity.this, "Cannot Log in Please check the form and try again!", Toast.LENGTH_LONG).show();
}
}
});
}
});
}
}
答案 0 :(得分:1)
使用两个不同的集合,针对每种类型的用户一个都不是一个灵活的解决方案。另一个更简单的解决方案可能是:
Firestore-root
|
--- users (collection)
|
--- uid (document)
| |
| --- email: "student@email.com"
| |
| --- password: "********"
| |
| --- type: "student"
|
--- uid (document)
|
--- email: "teacher@email.com"
|
--- password: "********"
|
--- type: "teacher"
看,我已经将用户的类型添加为用户对象的属性。现在,要根据电子邮件地址获取用户,请使用以下代码行:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference usersRef = rootRef.collection("users");
Query query = usersRef.whereEqualTo("email", "student@email.com")
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
String email = document.getString("email");
String password = document.getString("password");
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(/* ... */);
}
}
}
});
用户通过身份验证后,要将用户重定向到相应的活动,请使用以下代码行:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
usersRef.document(uid).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
String type = document.getString("type");
if(type.equals("student")) {
startActivity(new Intent(MainActivity.this, Student.class));
} else if (type.equals("teacher")) {
startActivity(new Intent(MainActivity.this, Teacher.class));
}
}
}
}
});
答案 1 :(得分:0)
package com.example.myapplicationomostff
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.annotation.NonNull
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.tasks.OnCompleteListener
import com.google.android.gms.tasks.Task
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.firestore.DocumentSnapshot
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.QueryDocumentSnapshot
import com.google.firebase.firestore.QuerySnapshot
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_registy.*
class MainActivity : AppCompatActivity() {
var myauth =FirebaseAuth.getInstance()
var rootRef = FirebaseFirestore.getInstance()
var usersRef = rootRef.collection("users")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnlogin.setOnClickListener {
var email =txtemaillogin.text.toString().trim()
var password =txtpasslogin.text.toString().trim()
userlogin(email,password)
}
btnregistry.setOnClickListener{
var intent = Intent(this, chose::class.java)
startActivity(intent)
}
}
private fun userlogin(email: String, password: String) {
var query = usersRef.whereEqualTo("email",email)
query.get().addOnCompleteListener { task ->
if (task.isSuccessful) {
for (document in task.getResult()!!) {
var email1 = document.getString("email").toString()
var password1 = document.getString("password").toString()
if(password1 == password
&& email1 == email
){
myauth.signInWithEmailAndPassword(email1, password1).addOnCompleteListener{
if(it.isSuccessful){
val uid = FirebaseAuth.getInstance().currentUser?.uid
if (uid != null) {
usersRef.document(uid).get().addOnCompleteListener { task ->
if (task.isSuccessful) {
val document = task.result
if (document != null) {
if (document.exists()) {
val type = document.getString("type")
if (type == "doc") {
startActivity(Intent(this@MainActivity,docpage::class.java))
} else if (type == "eng") {
startActivity(Intent(this@MainActivity, engpage::class.java))
}else if (type=="mos"){
startActivity(Intent(this@MainActivity, chosepage::class.java))
}
}
}
}
}
}
Toast.makeText(this,"ok",Toast.LENGTH_LONG).show()
}else{
Toast.makeText(this,"erorr",Toast.LENGTH_LONG).show()
}
}
}else{
Toast.makeText(this,"مش مسجل ف الموقع ",Toast.LENGTH_LONG).show()
}