Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch Android Firebase signUpWithEmailAndPassword error code?

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;
  // ...
});
like image 225
Bobby Avatar asked Mar 09 '23 08:03

Bobby


2 Answers

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


                    }
                }

            });
like image 194
Sagar Raut Avatar answered Apr 08 '23 23:04

Sagar Raut


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 the createUserWithEmailAndPassword 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.

like image 23
AL. Avatar answered Apr 08 '23 21:04

AL.