Sign up new users

Now it is time to explore the sign-up process. When the user opts to create an account, we need two pieces of primary information, which is email and password. It might sound a little humorous, but the method we need to use for creating the account is createUserWithEmailAndPassword. The method is named because of the functionality and here we need to attach an addOnCompleteListner interface with its callbacks:

mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Successfully account created
Log.d(TAG, "Accounted creation:success");
FirebaseUser user = mAuth.getCurrentUser();
// Your UI logic can be executed now
updateUI
(user);
} else {
// In case of acount creation failed.
Log.w(TAG, "Account creation:failure", task.getException());
Toast.makeText(MainActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
updateUI
(null);
}

// ...
}
});

This code is self-explanatory and if the task is successful, then we will have a FirebaseUser object as a result; if it is not, then it will show an error message on a simple toast. Remember, you can pass the email received from EditText or Email and password string.