I see online in JavaScript documentation you can catch the error code returned from the createUserWithEmailAndPassword
function to determine whether it's a email already used, password too weak etc. How do I do this in Java?
This in JavaScript can tell which error it is.
firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// ...
});
see this code for your reference this might be the exact answer yo want, catch exception if task not successful as shown in below code,
mAuth.createUserWithEmailAndPassword(mUserEmail, mPassword)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(LOG_TAG, getString(R.string.log_message_auth_successful) + " createUserWithEmail:onComplete:" + task.isSuccessful());
// if task is not successful show error
if (!task.isSuccessful()) {
mAuthProgressDialog.dismiss();
try {
throw task.getException();
} catch (FirebaseAuthUserCollisionException e) {
// show error toast ot user ,user already exist
} catch (FirebaseNetworkException e) {
//show error tost network exception
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
Toast.makeText(CreateAccountActivity.this, R.string.log_error_occurred,
Toast.LENGTH_LONG).show();
} else {
// successfully account created
// now the AuthStateListener runs the onAuthStateChanged callback
}
}
});
I think task.getException()
is the counterpart in Android for the errorCode you're looking for. The example in the docs shows it:
Sign up new users
Create a new
createAccount
method which takes in an email address and password, validates them and then creates a new user with thecreateUserWithEmailAndPassword
method.mAuth.createUserWithEmailAndPassword(email, password) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { Log.d(TAG, "createUserWithEmail:onComplete:" + task.isSuccessful()); // If sign in fails, display a message to the user. If sign in succeeds // the auth state listener will be notified and logic to handle the // signed in user can be handled in the listener. if (!task.isSuccessful()) { Toast.makeText(EmailPasswordActivity.this, R.string.auth_failed, Toast.LENGTH_SHORT).show(); } // ... } });
Add a form to register new users with their email and password and call this new method when it is submitted. You can see an example in our quickstart sample.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With