Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Re-Send Firebase email verification

I have a question which looks silly but I can't find answer anywhere. I have a simple signup iOS procedure which relies on Firebase Authentication SDK.

At a certain point after the user is created with:

FIRAuth.auth()?.createUser(withEmail: userName!, password: password!)

right after that I sent my user a verification email:

FIRAuth.auth()?.currentUser?.sendEmailVerification(completion:
                        {(error) in
                        if error == nil
                            {self.showSuccessPopUp()} 
                        else
                            {self.showErrorPopUp()}
                    })

Everything works more than fine, no problem at all.

My question is: my user receives the email and - for any reason - didn't click on the autogenerated confirmation link. Later on he open the app again and - forgetting that he already register once - tries to signup with the same email address.

Firebase just says that there's already an user created with that email address - as per documentation the user is created even if not 'active' -, therefore I'd like to give my users the option to have a "Resend verification email".

I've been digging into Firebase API documentation without a solution. Does anyone have ever had the same 'issue' ?

Thanks for any help

like image 329
Alex Avatar asked Apr 13 '17 07:04

Alex


2 Answers

Though late I would answer this in two scenarios:

1: You successfully called createUser, but when the user opens the app again, firebase.auth() says they are not signed in

In this case, the account exists with a password, so you will need to send a 'reset password' email, not an authentication email

2: You successfully called createUser, but when the user opens the app again, firebase.auth() says they ARE signed in

In this case, they are logged in, but not verified. Use

firebase.auth().currentUser.reload() // reloads user fields, like emailVerified:
if (!firebase.auth().currentUser.emailVerified) {
    //resend verification email
} else {
     //login
}

pardon my use of javascript but should be easy enough to translate

like image 80
Nth.gol Avatar answered Oct 01 '22 04:10

Nth.gol


This question already has a short answer but I'll add my answer as I was facing the same problem. So there's already an user created with that email address. Later on the user opens the app again and wants to resend the email verification, but this time you don't create the user with email as there's already an user created in firebase with same email address, instead you can signing the user through firebase first as firebase already has user's details, if successful then get the current user from firebase. Now you can give your users the option to have a "Resend verification email" if they haven't verified yet. See the code below to get clear idea.

The following code assumes that user receives the email and - for any reason - didn't click on the autogenerated confirmation link. Later on he open the app again and - forgetting that he already register once - tries to signup with the same email address. Therefore you want give users the option to have a "Resend verification email".

firebaseAuth.signInWithEmailAndPassword(username, password)
                            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                                @Override
                                public void onComplete(@NonNull Task<AuthResult> task) {
                                    if (task.isSuccessful()) {
                                        firebaseUser = firebaseAuth.getCurrentUser();
                                        firebaseUser.reload(); // Here you finally get the user, now you can send verification mail again.
                                        if(firebaseUser.isEmailVerified()) {
                                            // TODO
                                        }else {
                                            // TODO
                                            Toast.makeText(LoginActivity.this, "Please verify your email first!", Toast.LENGTH_LONG).show();
                                        }
                                    }
                                }
                            });

Now set a button Resend or something Here's the code for that

resend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(firebaseUser!=null){
                firebaseUser.reload();
                if(!firebaseUser.isEmailVerified()) {
                    firebaseUser.sendEmailVerification();
                    Toast.makeText(LoginActivity.this, "Email Sent!", Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(LoginActivity.this, "Your email has been verified! You can login now.", Toast.LENGTH_LONG).show();
                }
            }
        }
    });
like image 30
Sambit Mallick Avatar answered Oct 01 '22 04:10

Sambit Mallick