How to check if user is already registered in firebase?

Adam Jefer :

I want to verify if the user is already registered. Here is my code. Please help. Thanks.

fAuth           =  FirebaseAuth.getInstance();

    signUpBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if((validateName()||validateLastName()||validateEmail()||validatePassword()||validateRepeatPassword())!= true){
                String email = uEmail.getText().toString().trim();
                String password = uPassword.getText().toString().trim();
                fAuth.createUserWithEmailAndPassword(email,password);
                Toast.makeText(SignUpActivity.this,"Welcome!",Toast.LENGTH_SHORT).show();
                openPhoneActivity();
            }else {
                //here will be toast with something like "You are  already registered"
            }
            }
        });
Peter Haddad :

The method createUserWithEmailAndPassword will check if an email already exists inside firebase authentication. From the docs:

FirebaseAuthUserCollisionException thrown if there already exists an account with the given email address

You can use addOnCompleterListener() to know if creation was successful or not:

                auth.createUserWithEmailAndPassword(email, password)
                        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                if (!task.isSuccessful()) {
                                   Toast.makeText((getApplicationContext(), "Authentication failed: " + task.getException().getMessage(),Toast.LENGTH_SHORT).show();

https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseAuth#public-taskauthresult-createuserwithemailandpassword-string-email,-string-password

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=371770&siteId=1