简体   繁体   中英

How to check if user is already registered in firebase?

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"
            }
            }
        });

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

 auth.createUserWithEmailAndPassword(email, password) .addOnCompleteListener(CurrentActivity.this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { Toast.makeText(SignupActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show(); // 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(SignupActivity.this, "Authentication failed." + task.getException(), Toast.LENGTH_SHORT).show(); } else { //Do something here startActivity(new Intent(CurrentActivity.this, RedirectActivity.class)); finish(); } } });

An addOnCompleterListener() will throw FirebaseAuthUserCollisionException thrown if there already exists an account with the given email address. You can check if email is already registered by following code.

mAuth.createUserWithEmailAndPassword(signup_email, signup_password)
                            .addOnCompleteListener(SignupActivity.this, new OnCompleteListener<AuthResult>() {
                                @Override
                                public void onComplete(@NonNull Task<AuthResult> task) {
                                    if (task.isSuccessful()) {
                                        // Signup success
                                      
                                        Toast.makeText(SignupActivity.this,"Signup Successful!", Toast.LENGTH_SHORT).show();
                                        
                                    } else {
                                    
                                        // If signup fails, and user emial is already registered.
                                         if (task.getException() instanceof FirebaseAuthUserCollisionException) {
                                         
                                            Toast.makeText(SignupActivity.this,"Provided Email Is Already Registered!", Toast.LENGTH_SHORT).show();
                                            
                                        }

                                    }
                                }
                            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM