1

I'm creating an Android application and am implementing the login / register functionality.

I'm at the stage where the register activity is successfully creating user entries in my Firebase application, however, I can't seem to track if the task was successful.

Users being created

private void startRegister() {
    String email = mEmailField.getText().toString();
    String password = mPasswordField.getText().toString();
    String confirmPassword = mConfirmPassword.getText().toString();

    // Check that fields are not empty
    if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password) || TextUtils.isEmpty(confirmPassword)) {

        Toast.makeText(Register.this, "Email, password or confirm password field cannot be empty.", Toast.LENGTH_LONG).show();
    } else if (!password.equals(confirmPassword)) {

        Toast.makeText(Register.this, "Password and confirm password should match", Toast.LENGTH_LONG).show();
    } else {

        mAuth.createUserWithEmailAndPassword(email, password).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
            @Override
            public void onSuccess(AuthResult authResult) {

                Toast.makeText(Register.this, "Success", Toast.LENGTH_LONG).show();
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {

                Toast.makeText(Register.this, "Failure", Toast.LENGTH_LONG).show();
            }
        });
    }
}

Both the if !task.isSuccessful() or else blocks ever get reached but the user is created in Firebase. Any ideas why I can't track the success/if it failed?

IN COMPARISON:

This is working in my login class.

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

                if (!task.isSuccessful()) {
                    Toast.makeText(Login.this, "Credentials error, user may not exist.", Toast.LENGTH_LONG).show();
                }
            }
        });
Jake Fauvel
  • 87
  • 10

2 Answers2

1

Hard to say what's going with the current way of implementation. Try adding a onSuccess directly

mAuth.createUserWithEmailAndPassword(email, pass).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
        @Override
        public void onSuccess(AuthResult authResult) {
           //done
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            //display toast if registering failed
            ToastRect.failed(RegisterActivity.this, getString(R.string.app_activities_error_text) 
        }
   });
bensadiku
  • 436
  • 3
  • 15
  • I've added how I've implemented the login - that is working, I was planning to implement this in the same way, however, I can't track the success of the user creation. – Jake Fauvel Apr 01 '19 at 17:32
  • Have you tried attaching a onSuccessListener directly? This is what i have on one of my projects and it works. Check the updated answer. – bensadiku Apr 01 '19 at 17:37
  • Yea, I've tried what you've just updated it to. Doesn't hit the the success message or the failure message. – Jake Fauvel Apr 01 '19 at 17:44
  • Probably because it's never calling `createUserWithEmailAndPassword`. Can you confirm your code is reaching that else statement where you call the `createUserWithEmailAndPassword` method? – bensadiku Apr 01 '19 at 17:47
  • As I mentioned, but just to confirm. The users _are_ being created in Firebase I could understand that logic if the users weren't created in the first place. Every use I have attempted to create is now in my Authentication tab in Firebase. – Jake Fauvel Apr 01 '19 at 17:54
  • I don't think that's possible. If your users are being created in Firebase then onSuccess would be triggered. Does it also fail to show messages with my code? Because if it's not working with my code, you'll probably want to file an issue. – bensadiku Apr 01 '19 at 17:56
  • 1
    I apologise, I had a AuthStateListener that was copied across from the login page. This appears to have been kicking in before I could handle the successful creation / failure. I should have copied the entire file but stackoverflow was stating it was a large amount of code so I avoided doing it. – Jake Fauvel Apr 01 '19 at 18:20
  • 1
    Glad you sorted it. Update the original post to reflect the fix. – bensadiku Apr 01 '19 at 18:21
  • I've added an 'answer' or in this case spotted a silly mistake in my code. Thank you very much for your help. – Jake Fauvel Apr 01 '19 at 18:27
0
public class Register extends AppCompatActivity {

private EditText mEmailField;
private EditText mPasswordField;
private EditText mConfirmPassword;
private Button mRegisterButton;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FirebaseApp.initializeApp(this);
    setContentView(R.layout.activity_register);

    mEmailField = findViewById(R.id.registerEmailField);
    mPasswordField = findViewById(R.id.registerPasswordField);
    mConfirmPassword = findViewById(R.id.registerConfirmPassword);
    mRegisterButton = findViewById(R.id.registerButton);
    mAuth = FirebaseAuth.getInstance();


    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {

            if (firebaseAuth.getCurrentUser() != null) {
                 startActivity(new Intent(Register.this, UploadActivity.class));
            }
        }
    };

    // https://stackoverflow.com/questions/10936042/how-to-open-layout-on-button-click-android
    Button register = (Button) findViewById(R.id.navigate_to_login);
    register.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent myIntent = new Intent(view.getContext(), Login.class);
            startActivityForResult(myIntent, 0);
        }
    });

    mRegisterButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startRegister();
        }
    });
}

@Override
protected void onStart() {
    super.onStart();

    mAuth.addAuthStateListener(mAuthListener);
}

private void startRegister() {
    String email = mEmailField.getText().toString();
    String password = mPasswordField.getText().toString();
    String confirmPassword = mConfirmPassword.getText().toString();

    // Check that fields are not empty
    if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password) || TextUtils.isEmpty(confirmPassword)) {

        Toast.makeText(Register.this, "Email, password or confirm password field cannot be empty.", Toast.LENGTH_LONG).show();
    } else if (!password.equals(confirmPassword)) {

        Toast.makeText(Register.this, "Password and confirm password should match", Toast.LENGTH_LONG).show();
    } else {

        mAuth.createUserWithEmailAndPassword(email, password).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
            @Override
            public void onSuccess(AuthResult authResult) {

                Toast.makeText(Register.this, "Success", Toast.LENGTH_LONG).show();
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {

                Toast.makeText(Register.this, "Failure", Toast.LENGTH_LONG).show();
            }
        });
    }
}

}

To confirm the FirebaseAuth.AuthStateListener() was kicking in. This was a bad copy and paste job from the my Login class. This was stopping me handle the successful user creation.

The fix then looked like:

public class Register extends AppCompatActivity {

private EditText mEmailField;
private EditText mPasswordField;
private EditText mConfirmPassword;
private Button mRegisterButton;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FirebaseApp.initializeApp(this);
    setContentView(R.layout.activity_register);

    mEmailField = findViewById(R.id.registerEmailField);
    mPasswordField = findViewById(R.id.registerPasswordField);
    mConfirmPassword = findViewById(R.id.registerConfirmPassword);
    mRegisterButton = findViewById(R.id.registerButton);
    mAuth = FirebaseAuth.getInstance();


    // https://stackoverflow.com/questions/10936042/how-to-open-layout-on-button-click-android
    Button register = (Button) findViewById(R.id.navigate_to_login);
    register.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent myIntent = new Intent(view.getContext(), Login.class);
            startActivityForResult(myIntent, 0);
        }
    });

    mRegisterButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startRegister();
        }
    });
}

@Override
protected void onStart() {
    super.onStart();
}

private void startRegister() {
    String email = mEmailField.getText().toString();
    String password = mPasswordField.getText().toString();
    String confirmPassword = mConfirmPassword.getText().toString();

    // Check that fields are not empty
    if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password) || TextUtils.isEmpty(confirmPassword)) {

        Toast.makeText(Register.this, "Email, password or confirm password field cannot be empty.", Toast.LENGTH_LONG).show();
    } else if (!password.equals(confirmPassword)) {

        Toast.makeText(Register.this, "Password and confirm password should match", Toast.LENGTH_LONG).show();
    } else {

        mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {

                    Toast.makeText(Register.this, "Password and confirm password should match", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

}

Jake Fauvel
  • 87
  • 10