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

7

我在使用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")
        }

1
嘿,Jesus!欢迎来到stackoverflow。我强烈建议您设置一个更具描述性的问题标题。 - adolfosrs
你是否在当前用户上调用了sendEmailVerification方法,然后点击发送到邮箱的验证链接? - bojeil
是的,我就在那里。它在我的注册视图控制器文件中。一旦您注册,它会在创建帐户时自动发送电子邮件。 - jesusnieto5413
我编辑了代码。 - jesusnieto5413
2个回答

13

在将用户登录系统之前,您先检查了其电子邮件是否已验证。

这是我使用的方法。

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

2

您在 FIRAuth.auth() 之后使用了合并运算符,这意味着当 FIRAuth.auth() 为 nil 时,以下方法调用将返回 nil。如果是这种情况,您与 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.")
    }
}

你可以通过以下方法更轻松地找到错误:

这样做可以让你更容易地找到错误。


即使已验证电子邮件,我仍然收到相同的“电子邮件未经验证”的提示。 - jesusnieto5413
@jesusnieto5413 那么,Firebase 的一些问题出现了,我无法提供更多帮助。 - Kametrixom
@jesusnieto5413 你找到解决方案了吗?我也遇到了同样的问题。我进行了验证,但isEmailVerified仍然返回False。 - EhsanR

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接