Firebase身份验证会创建用户,但不会将其信息添加到数据库中 [英] Firebase Authentication creates user but does not add their Info to database

查看:37
本文介绍了Firebase身份验证会创建用户,但不会将其信息添加到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一般不熟悉使用Firebase和ios开发.即使将用户的信息添加为经过身份验证的用户,我也无法将用户的信息添加到Firestore数据库中.有什么建议吗?

I am new to using firebase and ios development in general. I am having an issue with adding user's info to the firestore database even though they are being added as authenticated users. Any Suggestions?

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()

                }
            }
        }
    }

推荐答案

将代码更改为以下内容:

Change your code to the following:

// 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(添加文档时出错:\(err)")您可以确切地知道错误是什么.

Using this print statement print("Error adding document: \(err)") you can know exactly what the error is.

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

Also change your security rules to the following:

// 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;
    }
  }
}

这篇关于Firebase身份验证会创建用户,但不会将其信息添加到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆