3

I'm using the auth service from Firebase it works but I don't know how to handle the error codes of createUserWithEmailAndPassword() like auth/email-already-in-use or auth/invalid-email ,here you can see the error list https://firebase.google.com/docs/reference/js/firebase.auth.Auth#createUserWithEmailAndPassword

public void register(View target){
        EditText email  = (EditText) findViewById(R.id.editTextName);
        EditText pass  = (EditText) findViewById(R.id.editTextPass);
        Log.d("email",email.getText().toString());
        Log.d("pass",pass.getText().toString());
        auth.createUserWithEmailAndPassword(email.getText().toString(),pass.getText().toString())
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>(){
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if(task.isSuccessful()){
                        Toast.makeText(RegistroActivity.this, "success",
                                Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(RegistroActivity.this, "fail",
                                Toast.LENGTH_SHORT).show();
                    }

                }
            });

    }
adjuremods
  • 2,938
  • 2
  • 12
  • 17
AFS
  • 1,433
  • 6
  • 28
  • 52

1 Answers1

3
FirebaseAuth.getInstance().createUserWithEmailAndPassword("EMAIL", "PASSWORD")
   .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
       @Override
       public void onComplete(@NonNull Task<AuthResult> task) {
           if (!task.isSuccessful()) {
               if (task.getException() instanceof FirebaseAuthUserCollisionException) {
               // thrown if there already exists an account with the given email address
               } else if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
               // thrown if the email address is malformed
               } else if (task.getException instanceof FirebaseAuthWeakPasswordException) {
               // thrown if the password is not strong enough
               }
           }
       }
   });
Ognian Gloushkov
  • 2,669
  • 1
  • 20
  • 35
  • P.S: about FirebaseAuthUserCollisionException you can choose in firebase console of users can or not can to register more that a account by email. Authentication -> Method -> Advanced – Diego Venâncio Aug 15 '17 at 13:35