【问题标题】:How to verify a user's email address on iOS with Firebase?如何使用 Firebase 在 iOS 上验证用户的电子邮件地址?
【发布时间】:2016-10-29 21:29:03
【问题描述】:

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

【问题讨论】:

  • 天哪!欢迎来到stackoverflow。我强烈建议您设置一个更具描述性的问题标题。
  • 您是否在 currentUser 上调用 sendEmailVerification,然后单击通过电子邮件发送的电子邮件验证链接?
  • 是的,我是。它在我的注册视图控制器文件中。注册后,它会在创建帐户时自动发送电子邮件
  • 我编辑了代码。

标签: ios swift firebase firebase-authentication


【解决方案1】:

您甚至在登录之前检查了用户电子邮件是否已通过验证。

这对我有用。

    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 那么,firebase 方面出了点问题,不能帮助你更多
    • @jesusnieto5413 你找到解决方案了吗? , 我也有同样的问题。我进行了验证,但 isEmailVerified 仍然返回 False。
    猜你喜欢
    • 2016-11-03
    • 1970-01-01
    • 2013-02-20
    • 2017-12-13
    • 2020-08-19
    • 2017-12-22
    • 1970-01-01
    • 2022-01-14
    • 2019-09-17
    相关资源
    最近更新 更多