3

I've registered users with the createUserWithEmailAndPassword method and they are registered on my firebase project (I can see their info). But when I try to login with the created email and password the task.isSuccessful method is always returning false and the else statement is running every time.

Code for login and registration:

private Button buttonRegister;
private EditText editTextEmail;
private EditText editTextPassword;
private TextView textViewSignin;

private ProgressDialog progressDialog;
private FirebaseAuth firebaseAuth;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    progressDialog=new ProgressDialog(this);
    firebaseAuth= FirebaseAuth.getInstance();


    buttonRegister = (Button) findViewById(R.id.buttonRegister);

    editTextEmail=(EditText) findViewById(R.id.editTextEmail);
    editTextPassword=(EditText) findViewById(R.id.editTextPassword);

    textViewSignin=(TextView) findViewById(R.id.textViewSignin);

    buttonRegister.setOnClickListener(this);
    textViewSignin.setOnClickListener(this);

    if(firebaseAuth.getCurrentUser()!=null){
        //start the profile activity
        finish();
        startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
    }


}

private void registerUser(){
    String email=editTextEmail.getText().toString().trim();
    String password=editTextEmail.getText().toString().trim();

    if(TextUtils.isEmpty(email)){
        Toast.makeText(this, "Please enter E-mail first", Toast.LENGTH_SHORT).show();
        return;
    }

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

    progressDialog.setMessage("Registerring User......");
    progressDialog.show();


    firebaseAuth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    progressDialog.dismiss();
                    if(task.isSuccessful()){
                        finish();
                        startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
                    }
                    else{
                        Toast.makeText(MainActivity.this, "Unable to Register! Please try again.", Toast.LENGTH_SHORT).show();
                    }
                }
            });


}

@Override
public void onClick(View v) {
    if(v == buttonRegister){
        registerUser();
    }

    if(v == textViewSignin){
        finish();
        startActivity(new Intent(this, LoginActivity.class));
    }

  }
KENdi
  • 7,576
  • 2
  • 16
  • 31
hemant yadav
  • 147
  • 2
  • 11

3 Answers3

3

@Hemant Yadav

Have you checked what exception firebase give when it returns false? Please check what exception is occurred at that time by using below code:

task.getException() //which returns the exception that caused the Task to fail.

Check and update what exception you are getting so, we can help you or you can get the idea to resolve it?

Hemal Patel
  • 114
  • 1
  • 11
  • it is giving FirebaseAuthInvalidCredentialsException – hemant yadav Dec 18 '18 at 10:22
  • There might be invalid login credentials which you are trying to log in. Try to login with correct credentials. for more details visit below link: [FirebaseAuthInvalidCredentialisException](https://developers.google.com/android/reference/com/google/firebase/auth/FirebaseAuthInvalidCredentialsException) – Hemal Patel Dec 19 '18 at 06:13
  • If you are still getting same exception then try to find out error code which is get from exception which will help you to resolve your problem. For getting error code try to use below method: `String errorCode = ((FirebaseAuthInvalidUserException) task.getException()).getErrorCode();` – Hemal Patel Dec 19 '18 at 06:24
  • The error codes supported by FirebaseAuthInvalidUserException are as follows: • **ERROR_USER_DISABLED** – The account with which the email is associated exists but has been disabled. • **ERROR_USER_NOT_FOUND** – No account could be found that matches the specified email address. The user has either entered the wrong email address, has yet to create an account or the account has been deleted....... – Hemal Patel Dec 19 '18 at 06:25
  • • **ERROR_EMAIL_ALREADY_IN_USE** – Indicates that the user is attempting to create a new account, or change the email address for an existing account that is already in use by another account. • **ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL** – When attempting to sign in to an account using the signInWithCredential() method passing through an AuthCredential object, this error indicates that the email associated with the AuthCredential instance is already in use by another account that does not match the provided credentials. – Hemal Patel Dec 19 '18 at 06:26
  • • **ERROR_CREDENTIAL_ALREADY_IN_USE** – It is quite possible for one user to establish multiple accounts by using different authentication providers. As will be outlined in Linking and Unlinking Firebase Authentication Providers, Firebase provides the ability for these separate accounts to be consolidated together through a process of account linking. This error indicates that an attempt has been made to link a credential to an account that is already linked to another account. – Hemal Patel Dec 19 '18 at 06:26
  • These error codes are provided in the form of string objects containing the error code name. Testing for an error, therefore, simply involves performing string comparisons against the error code. – Hemal Patel Dec 19 '18 at 06:27
  • For more details check below links [Firebsae Auth Error Code](https://stackoverflow.com/questions/37859582/how-to-catch-a-firebase-auth-specific-exceptions/38244409#38244409) – Hemal Patel Dec 19 '18 at 06:29
1

It was a silly mistake from my side. I assigned email value to password field while registering the user.

String email=editTextEmail.getText().toString().trim();
String password=editTextEmail.getText().toString().trim();

So password was not actually assigned the password which I actually passed. Sorry for this very silly mistake.

hemant yadav
  • 147
  • 2
  • 11
0

Try this code

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

    if (task.isSuccessful()) {
        finish();
        startActivity(new Intent(this, MainActivity.class));
    }
    else {
        try {
            throw task.getException();
        } 
        catch (FirebaseAuthInvalidCredentialsException e) {
            Toast.makeText(getApplicationContext(), "Invalid Password", Toast.LENGTH_LONG).show();
        }
        catch (FirebaseAuthEmailException e){
            Toast.makeText(getApplicationContext(), "Invalid Email", Toast.LENGTH_LONG).show();
        }
        catch (FirebaseAuthException e){
            Toast.makeText(getApplicationContext(), "Invalid Credentials", Toast.LENGTH_LONG).show();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

}
});

for more detail visit this https://firebase.google.com/docs/auth/android/password-auth

ShehrozEK
  • 180
  • 1
  • 1
  • 6
  • Please recheck your email and password because this exception is thrown when one or more of the credentials passed to a signInWithEmailAndPassword method fail to identify and/or authenticate the user – ShehrozEK Dec 18 '18 at 12:47
  • I had checked it several times by creating many different accounts but it is giving the same error – hemant yadav Dec 18 '18 at 13:13