2

Image

I have three roles in my firebase database

  1. teacher
  2. Student
  3. coordinator.

In that, if I log in with teacher then it should check role from the database and open teacher activity. Same for student and coordinator.

** Code:**

firebaseAuth.signInWithEmailAndPassword(email, password)
                        .addOnCompleteListener(Login_Form.this, new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                if (task.isSuccessful()) {

                                    Intent intent = new Intent(Login_Form.this, Adminpage.class);
                                    progressDialog.dismiss();
                                    startActivity(intent);

                                } else {
                                    progressDialog.dismiss();
                                    Toast.makeText(getApplicationContext(), "Login Failed Or User Not Available", Toast.LENGTH_LONG).show();
                                }
Hasitha Jayawardana
  • 2,326
  • 4
  • 18
  • 36

2 Answers2

0

I have tried with this one

firebaseAuth.signInWithEmailAndPassword(email, password)
                        .addOnCompleteListener(Login_Form.this, new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                if (task.isSuccessful()) {

                                    Intent intent = new Intent(Login_Form.this, Adminpage.class);
                                    progressDialog.dismiss();
                                    startActivity(intent);

                                } else {
                                    progressDialog.dismiss();
                                    Toast.makeText(getApplicationContext(), "Login Failed Or User Not Available", Toast.LENGTH_LONG).show();
                                }
0

While login , you must be using either phone number or email Id . If so , then you can fetch the result using the entered email id .

 postRef.orderByChild("email").equalTo(email)
        .addListenerForSingleValueEvent(object : ValueEventListener {
            override fun onCancelled(p0: DatabaseError) {

            }

            override fun onDataChange(dataSnapshot: DataSnapshot) {
                if (dataSnapshot.exists()) {
                    val userData: HashMap<String, User> = dataSnapshot.value as HashMap<String, User>
                    var  userId :String = ""

                    userData.keys.forEach {  userId =it}
                    Toast.makeText(this@SplashActivity,"Welcome Back . Happy to see you again" ,Toast.LENGTH_SHORT).show()
                    startHomeActivity()
                } 
            }
        })

In above , i have check if the email is present in the firebase database or not . Once it get success, then we have dataSnapshot in which you will get complete data associated with that email id .There you can check if its teacher , student etc. There is a check if dataSnapshot is empty or not just to ensure your dataSnapshot is having user data.

                val userData: HashMap<String, User> = dataSnapshot.value as HashMap<String, User>

This will give you the entire data of your entry .

Mini Chip
  • 949
  • 10
  • 12