Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase email verification WITHOUT creating and account

I'm creating an Android application that will deliver notifications. Users can opt-in with a phone number or an email account.

I just need to verify the email the user entered, I don't want to create a Firebase account

Firebase has a FirebaseUser#sendEmailVerification() but that will require to create an account.

In other words, I just want the email verification to be the same as the Phone verification, where Firebase will just send you a code or verification link.

Is there a way to leverage Firebase email verification without creating an account?

like image 634
NickPR Avatar asked Jan 26 '18 15:01

NickPR


1 Answers

For anyone trying to accomplish the same, here's how I was able to do it.

Go to Fibrebase console and enable Email/Password and Anonymous sign-in methods on the Authentication screen

Firebase Authentication screen

Then in you code, create an Anonymous user (this is what does the trick, because now you have a valid user to verify against), change the email, and then send a verification. After that, reload the Firebase user and check isEmailVerified()

mAuth = FirebaseAuth.getInstance();
mAuth.signInAnonymously()
    .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if (task.isSuccessful()) {
                Log.d(TAG, "signInAnonymously:success");
                if (mAuth.getCurrentUser().isEmailVerified() == false) {
                    mAuth.getCurrentUser().updateEmail("<MAIL YOU WANTO TO VERIFY HERE>");
                    mAuth.getCurrentUser().sendEmailVerification();
                    Log.e(TAG, "mail sent.....................................");
                }

                //updateUI(user);
            } else {
                // If sign in fails, display a message to the user.
                Log.w(TAG, "signInAnonymously:failure", task.getException());
                Toast.makeText(getApplicationContext(), "Authentication failed.",
                        Toast.LENGTH_SHORT).show();
            }
        }
    });

Here's the reloading part:

mAuth.getCurrentUser().reload()
        .addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                Log.e(TAG,( mAuth.getCurrentUser().isEmailVerified() ? "VERIFIED" : "Not verified"));
            }
        });
like image 98
NickPR Avatar answered Oct 31 '22 11:10

NickPR