【发布时间】: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