如何使用Firebase在iOS上验证用户的电子邮件地址? [英] How to verify a user's email address on iOS with Firebase?

查看:97
本文介绍了如何使用Firebase在iOS上验证用户的电子邮件地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直坚持使用Firebase进行电子邮件验证.我到处寻找指导,但没有帮助.用户验证他的电子邮件后,我的代码仍会打印出尚未验证的用户.我仍在尝试适应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")
        }

推荐答案

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

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

这对我有用.

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

这篇关于如何使用Firebase在iOS上验证用户的电子邮件地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆