【问题标题】:Prevent duplicate users in Firestore IOS防止 Firestore IOS 中的重复用户
【发布时间】:2021-06-27 15:46:08
【问题描述】:

我通过 Firebase 拥有三种不同的登录选项:电子邮件、Facebook、谷歌。我正在使用 Firestore 来存储用户信息,并且不想有重复的用户。当用户“注册”一个新帐户时,我想检查该 uid 是否已被使用。这就是我所拥有的,但它仍然无法正常工作(您可以拥有具有相同帐户和 uid 以及所有内容的重复用户):

    func addUserToFirestore(_ firstName:String, _ lastName:String, _ uid:String, _ email:String) {
        let db = Firestore.firestore()
        let docRef = db.collection("users").document("uid")
        // check if user exists in firestore
        docRef.getDocument { (document, error) in
            if let document = document {
               if document.exists {
                // user exists. send to chats screen.
                print("User already exists. Document data: \(String(describing: document.data()))")
                self.transitionToConvo()
              } else {
                // user does not exits. create a new user
                 print("Document does not exist. Create new user.")
                db.collection("users").addDocument(data: ["firstname":firstName, "lastname":lastName, "uid":uid, "email":email ]) { (error) in
                        print("New user created in Firestore")
                    self.transitionToConvo()
                        
                        if error != nil {
                            // Show error message
                            print("Error saving user data to Firestore")
                        }
                    }
              }
            }
        }
    }

在此处调用它(对于 facebook、google 和电子邮件略有不同)。这是用于 Facebook 注册/登录:

Auth.auth().signIn(with: credential, completion: { (user, error) in
            let firstName = user?.user.displayName
            let email = user?.user.email
            let lastName = ""
            guard let uid = user?.user.uid else { return }
            
            if let err = error {
                print("Failed to create a Firebase User with Google account: ", err)
                return
            } else {
                // Successfully logged in
                print("Successfully logged into Firebase with Google uid: ", uid, "Now add user to Firestore if user is new.")
                
                // check if user already exists
                **self.addUserToFirestore(firstName ?? "", lastName, uid, email ?? "")**
                    }
                })
            }

如您所见,两个用户的 uid 相同: user 1 user 2 我怎样才能让它没有重复的用户?任何帮助将不胜感激!

【问题讨论】:

    标签: swift xcode authentication google-cloud-firestore duplicates


    【解决方案1】:

    这里有几个问题。

    获取用户文档

    您正在调用let docRef = db.collection("users").document("uid"),它正在获取 ID 为“uid”的文档。您需要传入您的用户 ID。

    let db = Firestore.firestore()
    let docRef = db.collection("users").document(uid)
    

    创建一个新文档

    您使用的是addDocument,因此 Cloud Firestore 会创建一个自动生成的 ID,该 ID 与您的用户 ID 无关。详情请查看https://firebase.google.com/docs/firestore/manage-data/add-data#set_a_document

    docRef.setData(data: [
      "firstname":firstName,
      "lastname":lastName,
      "uid":uid,
      "email":email
    ])
    

    【讨论】:

    • 对于addDocument,如果else之后用户不存在,我想创建一个新用户。我还应该setData吗?
    • 我使用的确切代码是:db.collection("users").document(uid).setData(["firstname":firstName, "lastname":lastName, "uid":uid, "email":email]) { err in if err != nil { // Show error message print("Error saving user data to Firestore") } else { print("New user created in Firestore") self.transitionToConvo() } }
    • 我很高兴它对你有用。当您将setData 与文档引用一起使用时,如果该文档尚不存在,则会使用指定的 ID 创建它
    • 您可以将db.collection("users").document(uid) 替换为docRef,因为您已经在代码中设置了它(如我的回答所示)
    • 知道了,谢谢!您是否建议使用 uid 或电子邮件防止重复?我不确定是否应该使用 uid,因为我有多个注册选项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    • 2019-06-11
    • 1970-01-01
    • 2012-03-24
    • 2015-04-13
    • 2017-06-08
    相关资源
    最近更新 更多