При регистрации учетной записи с помощью Firebase auth я сохранял электронные письма в 2 категориях Учитель и Ученик.Я добавляю электронные письма в Firestore 2 разных категории Учитель и Ученик с email
и password
.Когда я вхожу в систему, я хочу проверить, принадлежит ли электронная почта к какой категории (учитель или ученик). Как я могу это сделать, я попытался выполнить запрос firesere 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();
}
}
});
}
});
}
}