【问题标题】:iOS Firebase: Retrieve and match database code to pass through authenticationiOS Firebase:检索并匹配数据库代码以通过身份验证
【发布时间】:2017-11-19 14:13:40
【问题描述】:

我正在尝试为管理员创建一个身份验证安全代码以登录/注册。如果用户通过存在于 firebase 数据库中的代码,我想要做的是登录以执行 segue。所以现在它不起作用,因为错误警报框不断弹出事件我输入了正确的安全码。

let alertController = UIAlertController(title: "Security Code?", message: "Please enter your dedicated security code", preferredStyle: .alert)

            let confirmAction = UIAlertAction(title: "Next", style: .default) { (_) in
                    let code = alertController.textFields?[0].text

                    let scref=Database.database().reference();

                scref.queryOrdered(byChild: "securitycode").queryEqual(toValue: code).observe(.value, with: { (snapshot) in

                    if (snapshot.exists())
                    {

                        Auth.auth().signIn(withEmail: self.emailText.text!, password: self.passwordText.text!, completion: { (user, error) in
                            if user != nil
                            {
                                //if sign in sucessful
                                self.performSegue(withIdentifier: "segueadmin", sender: self)
                            }   else
                                {
                                    if let myError = error?.localizedDescription{
                                        print(myError)


                                        let alertController = UIAlertController(title: "Error!", message: myError, preferredStyle: UIAlertControllerStyle.alert)
                                        alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default) {
                                            UIAlertAction in
                                            // Insert code to run on button click below
                                            self.printYes()
                                        })
                                        self.present(alertController, animated: true, completion: nil)
                                    }else{
                                        print("ERROR")
                                    }
                                }

                            })}


            let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }
            alertController.addTextField { (textField) in
                textField.placeholder = "Enter Code:"
            }


            //adding the action to dialogbox
            alertController.addAction(confirmAction)
            alertController.addAction(cancelAction)

            //finally presenting the dialog box
            self.present(alertController, animated: true, completion: nil)
        }

    }

【问题讨论】:

  • 好的,你有什么问题?
  • 即使我输入了正确的安全码也无法通过

标签: swift firebase firebase-realtime-database


【解决方案1】:

我认为查询数据库时可能会出现问题。

试试这个代码:

scref.child("securitycode").child(code).observe(.value, with: { (snapshot) in 
 if (snapshot.exists()) {
    ...//Do your thing
 }

您的目标是参考 /securitycode/{code} ,如果它存在,那么您的数据库中似乎存储了一个正确的值,因此您可以继续。如果不是,用户将无法继续。

【讨论】:

    【解决方案2】:

    哎呀!对我太不小心了!这是因为我没有很好地构造数据,忘记在 Firebase 中为数据添加“”。

    所以 firebase 数据库应该是:

    代码应为:

           ref=Database.database().reference().child("securitycode")
    
            ref.queryOrdered(byChild: "code")
                .queryEqual(toValue: self.textf.text)
                .observe(DataEventType.value, with: { (snapshot) in
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-13
      • 2017-01-29
      • 2019-04-19
      • 1970-01-01
      • 2019-09-08
      • 2018-04-01
      相关资源
      最近更新 更多