Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to verify a user's email address on iOS with Firebase?

I'm stuck with email verification with firebase. I've looked around for guidance but no help. After the user verifies his email, my code still prints out the user has not been verified. I'm still trying to get used to the syntax of firebase. Here is my code:

if FIRAuth.auth()?.currentUser!.emailVerified == true{
    FIRAuth.auth()?.signInWithEmail(email.text!, password: passsword.text!, completion: {
        user, error in

        if error != nil{
            print("Email/password is wrong or user does not exist")
        }else{
            print("Successful login.")
        }
    })
}else{
    print("Please verify your email.")
}

here is my code for the sign up section:

    let eduEmail = email.text
    let endInEdu = eduEmail?.hasSuffix("my.utsa.edu")

    if endInEdu == true {

        FIRAuth.auth()?.createUserWithEmail(email.text!, password: passsword.text!, completion: {
            user, error in

            if error != nil{
                let alert = UIAlertController(title: "User exists.", message: "Please use another email or sign in.", preferredStyle: UIAlertControllerStyle.Alert)
                alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
                self.presentViewController(alert, animated: true, completion: nil)

                print("Email has been used, try a different one")
                }else{

 FIRAuth.auth()?.currentUser!.sendEmailVerificationWithCompletion({ (error) in
                      })


                let alert = UIAlertController(title: "Account Created", message: "Please verify your email by confirming the sent link.", preferredStyle: UIAlertControllerStyle.Alert)
                alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
                self.presentViewController(alert, animated: true, completion: nil)








                print("This is a college email and user is created")




            }


        })


    }else{
        print("This is not a my.utsa.edu email")
        }
like image 579
jesusnieto5413 Avatar asked Jun 27 '16 18:06

jesusnieto5413


People also ask

Is Firebase email Authentication free?

Prices are per successful verification. On the Blaze plan, Phone Authentication provides a perpetual free tier. The first 10K verifications for both instances (USA, Canada, and India and All other countries) are provided for free each month. You are only charged on usage past this free allotment.


2 Answers

You checked if the user email was verified even before signing them in.

This is what worked for me.

    FIRAuth.auth()?.signInWithEmail(txtUsername.text!, password: txtPassword.text!) {
        (user, error) in
        if let user = FIRAuth.auth()?.currentUser {
            if !user.emailVerified{
                let alertVC = UIAlertController(title: "Error", message: "Sorry. Your email address has not yet been verified. Do you want us to send another verification email to \(self.txtUsername.text).", preferredStyle: .Alert)
                let alertActionOkay = UIAlertAction(title: "Okay", style: .Default) {
                    (_) in
                        user.sendEmailVerificationWithCompletion(nil)
                }
                let alertActionCancel = UIAlertAction(title: "Cancel", style: .Default, handler: nil)

                alertVC.addAction(alertActionOkay)
                alertVC.addAction(alertActionCancel)
                self.presentViewController(alertVC, animated: true, completion: nil)
            } else {
                print ("Email verified. Signing in...")
            }
        }
    }
like image 98
happy_sisyphus Avatar answered Oct 24 '22 06:10

happy_sisyphus


You are using the coalescing operator after FIRAuth.auth() which means the following method call will return nil when FIRAuth.auth() was nil. If this is the case, your comparison with true will fail, since nil is not true.

I suggest you to refactor your code like this for easier debugging:

guard let auth = FIRAuth.auth(), user = auth.currentUser else {
    print("No auth / user")
}

guard user.emailVerified else {
    print("Email not verified")
    return
}

guard let email = email.text, password = passsword.text else {
    print("No email or password")
    return
}

auth.signInWithEmail(email, password: password) { user, error in
    if let error = error {
        print("Email/password is wrong or user does not exist, error: \(error)")
    } else {
        print("Successful login.")
    }
}

You should find your error easier like this.

like image 45
Kametrixom Avatar answered Oct 24 '22 06:10

Kametrixom