如何使用Firebase在iOS上验证用户的电子邮件地址?

时间:2016-06-27 18:53:00

标签: ios swift firebase firebase-authentication

我坚持使用firebase进行电子邮件验证。我四处寻找指导但没有帮助。在用户验证他的电子邮件后,我的代码仍然打印出用户尚未验证。我还在尝试习惯firebase的语法。这是我的代码:

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

这是我的注册部分代码:

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

2 个答案:

答案 0 :(得分:14)

您已检查用户电子邮件是否在签名之前已经过验证。

这对我有用。

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

答案 1 :(得分:2)

您在FIRAuth.auth()之后使用合并运算符,这意味着当nil为零时,以下方法调用将返回FIRAuth.auth()。如果是这种情况,则与true的比较将失败,因为nil不是true

我建议您像这样重构代码以便于调试:

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

你应该更容易发现你的错误。