我有一个使用 Firebase 的注册流程。当我检查数据库中是否已存在电子邮件时,如下所示:
refUsers.queryOrdered(byChild: "email").queryEqual(toValue: emailText).observeSingleEvent(of: .value, with: { snapshot in
if (snapshot.value is NSNull) {
print("Unique email")
// Move to Password View.
let passwordViewController = self.storyboard?.instantiateViewController(withIdentifier: "PasswordViewController") as! PasswordViewController
self.navigationController?.present(passwordViewController, animated: true, completion: nil)
// Pass the emailText to the last View of the flow.
self.singleton.sharedInstance.emailText = emailText!
}
else {
print("Duplicate email")
}
})
问题是,我没有权限查看数据库中的/users,因为我的规则是:
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
我知道我可以使用 Auth.auth().createUser
查找电子邮件是否重复,但我在注册流程中检查的不仅仅是电子邮件。我也对唯一用户名使用相同的方法。我怎样才能做到这一点?
最佳答案
如您所见,这不是最好的方法。您不应该手动检查电子邮件是否已经存在 - Firebase 可以在用户注册时为您做这件事,您为什么不想使用它?
您需要的是一种不同的方法。我现在可以想到两种方法:
首先:
您可以向 Firebase 添加新规则,例如:
{
"rules": {
"usernames": {
".read": true,
".write": "auth != null"
},
"emails": {
".read": true,
".write": "auth != null"
}
}
}
您在这里所做的是创建一个名为 usernames
的新节点,每个用户都可以访问和阅读它。
在这里,您应该保留注册用户拥有的所有用户名的副本,并在注册时检查用户名是否已在此节点内。
第二种方式:
您可以稍微修改一下注册流程,让用户无需用户名即可注册。创建帐户后,您让他们设置用户名。有了一个很好的流程,它看起来就像同一个注册表单。
更新
使用上述规则,用户应该能够在不注册的情况下从 emails
和 usernames
中读取信息。通过这种方式,您可以获取数据并比较电子邮件或用户名是否已在使用中。
只需确保在用户注册时将他的电子邮件和用户名插入到这两个节点中即可。
关于ios -/用户的监听器失败 : permission_denied,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45217123/