【问题标题】:Firebase Authentication creates user but does not add their Info to databaseFirebase 身份验证创建用户但不将其信息添加到数据库
【发布时间】:2020-04-13 23:53:24
【问题描述】:

我是一般使用 firebase 和 ios 开发的新手。我在将用户信息添加到 firestore 数据库时遇到问题,即使他们被添加为经过身份验证的用户。有什么建议吗?

Auth.auth().createUser(withEmail: email, password: password) { (result, err) in
                if err != nil {
                    self.errorLabel.text = "Error Creating User"
                    self.errorLabel.alpha = 1
                } else {
                    let db = Firestore.firestore()

                    db.collection("users").addDocument(data: ["firstname":firstname, "lastname":lastname, "uid":result!.user.uid]) { (error) in

                        if error != nil {

                            self.errorLabel.text = "error saving user data"
                            self.errorLabel.alpha = 1
                        }
                    }

                    self.transitionScreens()

                }
            }
        }
    }

【问题讨论】:

  • 你遇到的错误是什么
  • @PeterHaddad 即使电子邮件和密码被注册为用户,用户的名字和姓氏也没有被添加到数据库中
  • 名字和姓氏都为零
  • 你检查过firestore安全规则吗?您可能已将写入规则设置为 false,如果是,则没有用户可以写入数据库
  • @PeterHaddad 我在控制台中将它们都打印出来了,所以我知道它们不是零

标签: swift firebase google-cloud-firestore firebase-authentication


【解决方案1】:

查看以下不同规则,您可以根据数据授予用户访问权限

service cloud.firestore {
  match /databases/{database}/documents {
  //allows all users to read and write, but dangerous as any one can flood your database
    match /public_collection/{document=**} {
        allow read, write: if true;
    }
//only read access
    match /public_read_collection/{document=**} {
        allow read: if true;
        allow write: if false;
    }
//prefered for storing users personal info, users can access only their data
    match /users/{userId} {
      allow read, write: if request.auth.uid == userId;
    }
//any authenticated user can access or write data
    match /posts/{documentId} {
      allow read, write: if request.auth.uid != null;
    }
  }
}

【讨论】:

    【解决方案2】:

    将您的代码更改为以下内容:

    // Add a new document with a generated ID
    var ref: DocumentReference? = nil
    ref = db.collection("users").addDocument(data: [
        "firstname": firstname,
        "lastname": lastname,
        "uid": result!.user.uid
    ]) { err in
        if let err = err {
            print("Error adding document: \(err)")
        } else {
            print("Document added with ID: \(ref!.documentID)")
        }
    }
    

    使用这个打印语句print("Error adding document: \(err)")你可以确切地知道错误是什么。

    还将您的安全规则更改为以下内容:

    // Allow read/write access to all users under any conditions
    // Warning: **NEVER** use this rule set in production; it allows
    // anyone to overwrite your entire database.
    service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
          allow read, write: if true;
        }
      }
    }
    

    【讨论】:

    • 谢谢!更改安全规则是问题所在。
    • 只在开发中使用这些规则,而不是在生产中使用
    • 我需要为新用户制定特定规则还是“允许读取,写入:如果 request.auth.uid == request.resource.data.author_uid”有效?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    • 1970-01-01
    • 1970-01-01
    • 2019-09-27
    • 1970-01-01
    • 2020-01-27
    • 1970-01-01
    相关资源
    最近更新 更多