4
votes

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

3
When you get the error saying the user exists, you can use the user entry credential to re-auth the user and then you have the current user to re-send the verification.danialmoghaddam
You cannot re-auth the user as it already exists. This means that FIRAuth.auth().currentUser is nilAlex
@Alex Did you find a solution to this problemSwapnil

3 Answers

8
votes

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

1
votes

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();
                }
            }
        }
    });
1
votes

This can be done using a firebase function with an email sending module (Nodemailer or Firebase Trigger Email extension for example)

The solution is in the firebase documentation See section Generate email verification link