【问题标题】:How to validate fields server side during user creation with Firebase Auth?如何在使用 Firebase Auth 创建用户期间验证字段服务器端?
【发布时间】:2020-02-09 21:56:59
【问题描述】:

我正在尝试使用 Firebase 身份验证来创建新用户,但我想在创建新帐户之前使用 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 失败。

【问题讨论】:

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


    【解决方案1】:

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

    您唯一能做的就是在创建帐户后使用Cloud Functions auth trigger 检查帐户属性,然后在出现问题时删除或停用帐户。

    【讨论】:

    • 我已经考虑过了。我可以使用此触发器检查 Firestore 中的 uid 是否有相应的写入,如果没有,可以删除/禁用帐户。然而,这个问题是 - 我如何让用户知道这个问题?显然,我想查明客户端确切字段的确切问题,并允许用户在尝试再次单击注册按钮之前修复该字段。
    • 没有简单的解决方案。你必须为此建立一些基础设施。使用云消息传递或编写他们以后可以阅读的文档。
    • 谢谢道格!想出了一些东西并在尝试它的过程中 - 我可以将注册过程分为两个步骤。第一步可以要求用户输入由 Firebase Auth 自动验证的电子邮件和密码。帐户创建成功后,我可以导航到下一个屏幕并要求他们输入可以使用 Firestore 安全规则验证的其他详细信息。它并不完美,但它似乎是最容易实现的,并且尽可能接近完美——特别是考虑到我刚刚开始编码!感谢您的帮助:)
    猜你喜欢
    • 2012-05-13
    • 1970-01-01
    • 2019-01-16
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多