ios - 如何在使用 Firebase Auth 创建用户期间验证字段服务器端?

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

我正在尝试使用 Firebase Auth 创建新用户,但我想在创建新帐户之前使用 Firestore 安全规则验证某些字段(模式匹配)。我该怎么做?

在 createUser(withEmail: , password:) 函数的完成处理程序中,我在成功创建帐户后对 Firestore 执行一些写入操作。

我遇到一个问题,有时由于 Firestore 安全规则(模式匹配),写入 Firestore 可能不会成功。在这种情况下,写入失败,但仍会创建新的用户帐户(因为正在完成处理程序中尝试写入)。

// Create User Method - Firebase Auth & Swift

Auth.auth().createUser(withEmail: self.emailTextField.text!, password: self.passwordTextField.text!) { (result, error) in
                    if error != nil {
                        print(error?.localizedDescription)
                    } else { 
                        let userName = [
                            userName:self.userNameTextField.text!
                        ]

// Writing field Data to Firestore

Firestore.firestore().collection("users").document(self.userNameTextField.text!).setData(userName) {(err) in
                            if err != nil {

// Rather than throwing a fatalError, how can I ensure new account creation is cancelled so that feedback can be given on the issue with entered field data?                              

fatalError()
                            }

我想确保不会创建用户帐户,以防因与 Firestore 安全规则冲突而导致写入 Firestore 失败。

最佳答案

Firebase 身份验证没有任何安全规则。目前无法在创建用户之前检查传入帐户属性是否有效。安全规则仅适用于从移动或网络客户端直接读取和写入 Cloud Firestore(或实时数据库或云存储)的数据。

你唯一能做的就是使用 Cloud Functions auth trigger创建帐户后检查帐户属性,如果出现错误,则删除或停用帐户。

关于ios - 如何在使用 Firebase Auth 创建用户期间验证字段服务器端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58357061/

相关文章:

ios - Swift 中的 UIView 子类 : IBOutlet is found nil while unwrapping

ios - 获取单个 App 电池使用情况 iOS

javascript - 按时间戳查询 firestore 中的数据

javascript - 如何从网络中的firestore存储下载多个图像

ios - 单击 TableView 某个部分中的任意位置后如何执行 segue?

iphone - 在两个或多个 iPhone 应用程序之间共享数据

ios - UITableViewCell 如何自己更改 textLabel 文本?

swift - 在 Swift 闭包中隐式使用 self

ios - 如何在 Firebase (Swift) 中使用安全规则限制子节点

ios - 在真实设备中测试临时配置文件 - IOS XCode