ios - 从 Firestore 中选择一个用户

标签 ios swift firebase google-cloud-firestore

我无法解决问题:在 Firestore 中有一个用户集合,其中有几个带有字段的文档(电子邮件:“String”,名称:“String”,密码:“String”)。在 textField 中,输入了电子邮件和密码,有必要编写一个函数,从输入的数据中选择 Firestore 中的特定用户。函数entryLogin():

func entranceLogin() {
    guard let email = emailTextField.text, let password = passwordTextField.text else{
        print("Form is not valid")
        return
    }

    let values1 = [email]
    let values2 = [password]
    var db = Firestore.firestore()
    db.collection("users").getDocuments() { (querySnapshot, err) in
        if let err = err {
            print("Error getting documents: \(err)")
        } else {
            for document in querySnapshot!.documents {
                db = document.value(forKey: "email = '\(values1)' && password = '\(values2)'") as! Firestore
                    print("\(db) => \(document.data())")
            }

             // self.dismiss(animated: true, completion: nil)
            }
        }
    }

最佳答案

按照您的方式将密码存储在数据库中是非常不安全的。 Firebase 已经为您提供了登录功能

Auth.auth().signIn(withEmail: email, password: password) { (user, error) in 
//.....
}

然后在您的数据库中不保存密码,只保存每个用户的用户 UID。然后您可以使用以下方法获取用户:

guard let uid = Auth.auth().currentUser.uid else { return }
Firestore.firestore().collection("users").whereField("userID", isEqualTo: uid).getDocuments() { snapshot, error in 
// Do user fetching here
}

关于ios - 从 Firestore 中选择一个用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51181140/

相关文章:

ios - 如何使用 swift 语言在 lldb 控制台中呈现/推送自定义 viewController

ios - 如何从通知中心删除 MPRemoteCommandCenter

ios - 将 AIR 应用程序移植到 iOS : do I need an iPad to debug the app?

ios - 使用自动布局调整 UIImageView 的大小

ios - SWIFT:来自 TableViewCell(原型(prototype)单元格)的模态 Segue 不工作

ios - swiftyJSON 检查值是否为空

android - FirebaseRecyclerAdapter 中的 FirebaseRecyclerAdapter() 无法应用于[错误的参数]

ios - firebase 3 我们可以在 firebase.database().goOffline() 之后登录吗?

javascript - Firestore - 如何像我们在 sql 中所做的那样获取集合中的记录总数

iphone - View 隐藏在 UINavigationBar iOS 7 下方