0

I want to login to Student Account and Teacher Account without using usertype.

public class Login extends AppCompatActivity implements View.OnClickListener {

    private TextView signUp;
    private Button login;
    private TextInputEditText email, password;


    private FirebaseAuth mAuth;
    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        signUp = findViewById(R.id.signup);
        signUp.setOnClickListener(this);

        login = findViewById(R.id.btnLogin);
        login.setOnClickListener(this);

        email = findViewById(R.id.email_input_login);
        password = findViewById(R.id.password_input_login);

        progressBar = findViewById(R.id.progressBar);

        mAuth = FirebaseAuth.getInstance();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.signup:
                startActivity(new Intent(this, Signup.class));
                break;
            case R.id.btnLogin:
                userLogin();
                break;
        }
    }

    private void userLogin() {
        String Email = email.getText().toString().trim();
        String Password = password.getText().toString().trim();

        if (Email.isEmpty()){
            email.setError("Email is required!");
            email.requestFocus();
            return;

        }
        if (!Patterns.EMAIL_ADDRESS.matcher(Email).matches()){

            email.setError("Please enter a valid email!");
            email.requestFocus();
            return;
        }
        if(Password.isEmpty()){
            password.setError("Password is required!");
            password.requestFocus();
            return;
        }
        if (Password.length() < 6){
            password.setError("Min password length is 6 characters!");
            password.requestFocus();
            return;

        }

        progressBar.setVisibility(View.VISIBLE);

        mAuth.signInWithEmailAndPassword(Email,Password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                progressBar.setVisibility(View.INVISIBLE);
                if (task.isSuccessful()){

                    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

                    if(user.isEmailVerified()) {

                        String uid = task.getResult().getUser().getUid();

                        FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
                        firebaseDatabase.getReference("USER").child(uid).child("usertype").addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot snapshot) {
                                int usertype = snapshot.getValue(Integer.class);
                                if (usertype == 1) {
                                    Intent in = new Intent(Login.this, MainActivity.class);
                                    startActivity(in);


                                } else if (usertype == 0) {
                                    Intent in = new Intent(Login.this, Example.class);
                                    startActivity(in);
                                }
                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError error) {
                            }
                        });

                    }
                    else{
                        user.sendEmailVerification();
                        Toast.makeText(Login.this, "Check your email to verify your account!", Toast.LENGTH_SHORT).show();
                    }

                }else{
                    Toast.makeText(Login.this, "Failed to Login! Please check your credentials", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
}

My Realtime database

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I'm not sure I understand the question. If you step through your code in a debugger, which exact line doesn't do what you expect it to do? – Frank van Puffelen Nov 07 '21 at 03:35
  • @Frank van Puffelen Hi what I wanted to do is I don't want to start from uid, I want to separate both student and teacher so when I login example USER >Student> uid then go to Main Activity if conditions met, same for the teacher. thanks – red daemon Nov 07 '21 at 08:34

0 Answers0