NickPR
NickPR

Reputation: 594

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?

Upvotes: 6

Views: 4454

Answers (2)

NickPR
NickPR

Reputation: 594

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

Upvotes: 7

Frank van Puffelen
Frank van Puffelen

Reputation: 598857

Both phone number verification and email verification are tied to a Firebase Authentication account. There is no way to use them with such an account, since the result of a verification is that a the relevant property (email_verified or phone_number) in the user account gets updated.

Upvotes: 0

Related Questions