While Registering an account using Firebase auth I stored emails in 2 categories Teacher and Student. I add emails to Firestore 2 different categories Teacher and Student wi
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()
}
Using two different collections, one for each type of your users isn't quite a flexible solutions. Another more simpler solution might be:
Firestore-root
|
--- users (collection)
|
--- uid (document)
| |
| --- email: "student@email.com"
| |
| --- password: "********"
| |
| --- type: "student"
|
--- uid (document)
|
--- email: "teacher@email.com"
|
--- password: "********"
|
--- type: "teacher"
See, I have added the type of the user as a property of your user object. Now, to get a user based on the email address, please use the following lines of code:
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(/* ... */);
}
}
}
});
Once the user is authenticated, to redirect the user to the corresponding activity, please use the following lines of code:
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));
}
}
}
}
});