如果用户属于列表

时间:2017-04-23 15:47:35

标签: android firebase firebase-authentication

我正在开发一个Android应用程序,它有两种类型的用户(用户和供应商)。我试图阻止供应商登录用户登录页面。遗憾的是,没有办法区分Firebase中不同类型的用户,因此我导入供应商电子邮件列表并检查输入的电子邮件是否符合该列表。

如果那么他就不会被允许登录其他他会。但是我的代码由于某种原因同时运行if和else语句,因此它向他显示错误并仍然让他进入。任何帮助都将非常感激。这是我的代码。

   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));
        }
    }
}

0 个答案:

没有答案