3
votes

Is there a way in Firebase to verify a user's email prior to adding the user to the database? My ultimate goal is to create a SignUp form where the user first enters the email address, then presses a "Verify Email address" button. At this point the verification email will be sent and the user will follow the link from within to confirm the email address. Now, when going back to the app, the user will have a continue button, and if the the email is not verified, the user will not be able to register, if contrary, the user will be able to register.

How do I go about this? So far all the documentation says that I must createUser so then I can use currentUser.sendEmailVerification, but obviously, I do not want create a user, before verification. I also thought about using a completion block, but I am not sure how to work that out, because the registration would have to be postponed until the user presses the continue button.

Thanks

Auth.auth().createUser(withEmail: email, password: password, completion: {(user: User?, error) in

            if error != nil {
                print(error!)
                return
            }

            guard let uid = user?.uid else {
                return
            }


            let ref = Database.database().reference(fromURL: "https://project/")
            let values = [
                "email": email,
                "userName": userName,
                "name": name,
                "birthDate": birthDate,
                "phoneNumber": phoneNumber]

            let userReference = ref.child("users").child(uid)
            userReference.updateChildValues(values, withCompletionBlock: {(err, ref) in
                if err != nil{
                    print(err!)
                    return
                }

                print("Successfully added user to database")
            })

            let usedUserReference = ref.child("users-Used").child(userName)
            usedUserReference.setValue(uid, withCompletionBlock: {(err, ref) in
                if err != nil{
                    print(err!)
                    return
                }

                print("Successfully added user to cannot be used-again list")
            })

        })
1
If you simply want to take action until a user has been verified, then take that action when the user signs in and that user's emailVerified property is true. You can't avoid the requirement of creating the user and sending the email verification request.Doug Stevenson

1 Answers

4
votes

You can't do this with Firebase Auth. The user needs to be created first, because that record is the "container" that gives the email verification the appropriate context about which user has actually been verified when they respond to the email.

In your app, you can check to see if a particular user account has been verified by using the emailVerified property. This allows you to give the end user different experience with respect to their verification.