Maarten Weyns
Maarten Weyns

Reputation: 455

Check if email and password are correct Firebase

I'm currently working on an app that shows users in a listview and when you click on a user, you get to see the details.

Now you can 'add' users by filling in editText field with his or her info. Now I want that only the person himself can add his or her info. I added a editText asking for your email and an editText asking for a password. These credentials should match an account previously created in the app in Firebase. I do not seem to accomplish it.

This is my code:

String email2 = mEmailField.getText().toString();
String pw = mPassword.getText().toString();

firebaseAuth.signInWithEmailAndPassword(email2, pw).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {

When the user fills in his email and password everything works, but when the incorrect password is entered, it works as well and that should not happen. What did I do wrong?

Upvotes: 1

Views: 2872

Answers (2)

Mudassir Ahmed
Mudassir Ahmed

Reputation: 51

    mAuth.signInWithEmailAndPassword(et_LogIn_Email.getText().toString(),et_LogIn_Pasword.getText().toString())
            .addOnCompleteListener((EntryActivity) requireActivity(), new OnCompleteListener<AuthResult>() {
                @SuppressLint("SetTextI18n")
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        Toast.makeText((EntryActivity) requireActivity(), "LogIn...", Toast.LENGTH_SHORT).show();
                        loadingDialog.stopLoading();
                        gotoUiPage();
                    } else {
                        Toast.makeText((EntryActivity) requireActivity(), "Error! try again...", Toast.LENGTH_SHORT).show();
                        tv_LogIn_warning.setText("Enter Carefully!...");
                        loadingDialog.stopLoading();
                    }
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    tv_LogIn_warning.setText(e.getMessage());
                }
            });

onFailureListener provide an exception by which you handle it.

Upvotes: 0

Saurabh Padwekar
Saurabh Padwekar

Reputation: 4064

Inside OnComplete check if the Authentication was successful.

 public void onComplete(@NonNull final Task<AuthResult> task) {
       if (task.isSuccessful()) {
          Toast.makeText(getContext(), "Authentication Successful", Toast.LENGTH_SHORT).show();
       } else {
          Toast.makeText(getContext(), "Authentication failed.",Toast.LENGTH_SHORT).show();
       }  
  }

Upvotes: 3

Related Questions