Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out if an email is already registered with Firebase Simple Login?

I am using firebase (with Angularfire) for an html5 phone app. The user inputs only their email in the start screen, and then, depending on whether that email is already registered or not, the user is redirected to the login or registration page, respectively. For step1, how can we query the firebase simple login to determine if the email is registered or not?

like image 283
Jarnal Avatar asked Feb 27 '14 19:02

Jarnal


People also ask

How do you check if email already exists in Firebase authentication Python?

EDIT: To check if user is 'made-up' or 'real', just send an email verification by calling FirebaseAuth. instance. currentUser. sendEmailVerification() and ask user to click the link sent in their email, if user is able to perform this action then email exists otherwise it's 'made-up'.

How do you check if email already exists in Firebase authentication in flutter?

Show activity on this post. try { _firbaseAuth. createUserWithEmailAndPassword( email: '[email protected]', password: 'password' ); } catch(signUpError) { if(signUpError is PlatformException) { if(signUpError. code == 'ERROR_EMAIL_ALREADY_IN_USE') { /// `[email protected]` has alread been registered. } } }


1 Answers

Well Following #Kato's and #MikePugh's commented suggestions I am gonna write this solution and it worked for me.

Solutio#1 (highly recommended)

 mAuth.fetchProvidersForEmail("[email protected]").addOnCompleteListener(new OnCompleteListener<ProviderQueryResult>() {
        @Override
        public void onComplete(@NonNull Task<ProviderQueryResult> task) {
            if(task.isSuccessful()){
          ///////// getProviders().size() will return size 1. if email ID is available. 
                task.getResult().getProviders().size();
            }
        }
    });

Solution#2

Create a dummy FirebaseUser object.

FirebaseUser firebaseUser = null;
private FirebaseAuth mAuth;

private void isEmailAlreadyUsed() {
    mAuth = FirebaseAuth.getInstance();

///// here I am gonna create dummy user at **Firebase** with the Entered email Id (Email to check against) 

    mAuth.createUserWithEmailAndPassword(mEmailView.getText().toString(), "dummypass")
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    Log.d(TAG, "createUser:onComplete:" + task.isSuccessful());


                    if (task.isSuccessful()) {
                        /////user do not exit so good to initialize firebase user.              
                    firebaseUser = task.getResult().getUser();
                    } else {
                        if(task.getException().getMessage().equals("The email address is already in use by another account.")) {
                          Log.d(TAG, "This email ID already used by someone else");     
                        }
                    }
                }
            });
}

Later on when user enter password and press SIGNUP button then you can just update password of the user which was set as dummypass.

   boolean isSignUpMade = false;
    if (firebaseUser != null)
        firebaseUser.updatePassword(password)
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        signUpCallProgress.setVisibility(View.VISIBLE);
                        if (task.isSuccessful()) {
                            isSignUpMade = true;
                            Log.d(TAG, "User password updated.");
                        } else {
                            isSignUpMade = false;
                            Log.d(TAG, "User password not updated.");
                        }
                    }
                });

You can see I have used isSingUpMade variable just to insure that user presses SINGUP button.

If user wants to leave without signing up than that dummy user must be deleted from FIREBASE server.

so here is the logic.

 @Override
public void onBackPressed() {
    if (!isSignUpMade) {
        if (firebaseUser != null)
            firebaseUser.delete().addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if (task.isSuccessful()) {
                        Log.d(TAG, "User account deleted.");
                    }
                }
            });
    }
    super.onBackPressed();
}
like image 193
Muhammad Adil Avatar answered Oct 27 '22 00:10

Muhammad Adil