1

I am working on an android app which has two type of users (user and vendors). I am trying to prevent vendors from signing in on the user sign in page. Sadly, there is no way of differentiating between different type of users in Firebase, therefore I am importing vendor email list and checking if the email entered meets that list.

If it does then he wont be allowed to sign in else he will. However my code runs both the if and else statements for some reason so it shows him the error and still signs him in. Any help will be greatly appreciated it. Here is my code.

   private void userLogin() {
        final String email = editTextEmail.getText().toString().trim();
        final String password = editTextPassword.getText().toString().trim();


        //checking if email and passwords are empty
        if (TextUtils.isEmpty(email)) {
            Toast.makeText(this, "Please enter email", Toast.LENGTH_LONG).show();
            return;
        }

        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "Please enter password", Toast.LENGTH_LONG).show();
            return;
        }

        mdata = new Firebase("https://uevent-f8ea1.firebaseio.com");



        //close this activity
    enter code here

        mdata.child("Doubleauthvendors").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot result) {
                List<String> lst = new ArrayList<String>(); // Result will be holded Here
                for (DataSnapshot dsp : result.getChildren()) {
                    lst.add(dsp.child("email").getValue().toString()); //add result into array list
                    Toast.makeText(getApplicationContext(),dsp.child("email").getValue().toString(),Toast.LENGTH_LONG).show();


                    if ((dsp.child("email").getValue().toString().equals(email))) {
                        new AlertDialog.Builder(Login.this)
                                .setTitle("Incorrect login credentials.")
                                .setMessage("The credentials you entered belong to another type of account. Would you like to try again?")

                                .setNegativeButton("Yes", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        // continue with delete
                                        editTextEmail.setText("");
                                        editTextPassword.setText("");

                                    }
                                })

                                .setPositiveButton("No", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        // continue with delete
                                        startActivity(new Intent(getApplicationContext(), AllLoginPage.class));
                                    }



                                })
                                .setIcon(android.R.drawable.ic_dialog_alert)
                                .show();
                    }
                    else{
                        progressDialog.setMessage("Logging in Please Wait...");
                        progressDialog.show();


                        //logging in the user
                        firebaseAuth.signInWithEmailAndPassword(email, password)
                                .addOnCompleteListener(Login.this, new OnCompleteListener<AuthResult>() {
                                    @Override
                                    public void onComplete(@NonNull Task<AuthResult> task) {
                                        progressDialog.dismiss();
                                        //if the task is successfull
                                        if (task.isSuccessful()) {
                                            //start the profile activity
                                            //finish();
                                            startActivity(new Intent(getApplicationContext(), Profile.class));

                                        } else {
                                            Toast.makeText(Login.this, "Please enter the correct email and password", Toast.LENGTH_LONG).show();
                                        }


                                    }
                                });

                    }
                }

            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {
            }


        });

        }


    @Override
    public void onClick(View view) {
        if (view == buttonSignIn) {
            userLogin();
        }

        if (view == textViewSignup) {
            finish();
            startActivity(new Intent(this, Registration.class));
        }
    }
}
AL.
  • 35,361
  • 10
  • 135
  • 270
Zain
  • 11
  • 4
  • I recently explained the difference between authentication and authorization. That might be helpful here too. See http://stackoverflow.com/questions/43546817/copy-this-file-to-your-authentication-server-firebase-custom-auth/43547921#43547921 – Frank van Puffelen Apr 23 '17 at 17:17

0 Answers0