首页 > 解决方案 > 如何使用firebase在xcode中验证电子邮件和密码

问题描述

我想使用 firebase 中的验证电子邮件验证使用电子邮件和密码登录的用户。

这是我的代码:

@IBAction func Login(_ sender: Any) {
    guard let email = txtUser.text, email != "",
        let password = txtPass.text, password != ""
        else {
            AlertController.showAlert(self, title: "Missing Info", message: "Please fill out all required fields")
            return
        }
    Auth.auth().signIn(withEmail: txtUser.text!, password: txtPass.text!, completion: { (authResult,error) in
        if error != nil{
            AlertController.showAlert(self, title: "Error", message: error!.localizedDescription)
        } else if authResult != nil {
            self.performSegue(withIdentifier: "SegueMode", sender: self)
        }
    })
}

标签: iosswiftfirebasefirebase-authentication

解决方案


这是我在项目中使用的功能(注意:根据您的要求更改!)

 func loginUser() {

    guard let email = txtUserName.text, let password = txtPassword.text else  { return }

    Auth.auth().signIn(withEmail: email, password: password, completion: { (user, error) in
        if let error = error {
            print(error.localizedDescription)
            self.lblSuccess.isHidden = false
            self.lblSuccess.textColor = UIColor.red
            self.lblSuccess.text = "Invalid Credential Please Check Your Email and password"

        }else if let user = Auth.auth().currentUser {
            let listVC = self.storyboard?.instantiateViewController(withIdentifier: "UserListVC") as! UserListVC
            print(user)
            self.lblSuccess.isHidden = false
            self.lblSuccess.textColor = UIColor.green
            self.lblSuccess.text = "Login SuccessFully!!"
            self.navigationController?.pushViewController(listVC, animated: true)
        }
    })
}

推荐阅读