我正在寻找一种安全的方法来在 Firebase 中创建新帐户之前检查用户名是否唯一。
由于我在 Unity 中使用函数 CreateUserWithEmailAndPasswordAsync(),Firebase 内置机制会检查电子邮件地址是否唯一。
因此,我必须在创建新用户之前阅读所有现有的用户名。 到目前为止,我的想法是将所有用户名(仅此而已)另外存储在一个单独的区域“用户名”中,该区域具有以下简单的访问规则,允许每个用户(也包括未经身份验证的新用户)访问此数据。 实际用户数据存储在用户下,我限制了实际所有者的访问权限。
由于这是我第一次尝试为潜在的高效应用程序设置安全且有意义的方式,我将不胜感激获得对我的预期方法的反馈。
{
"rules": {
"usernames": {
// can be accessed without authentication, so before the new user is created.
".read": true,
".write": true
},
"users": {
"$user_id": {
// grants write access to the owner of this user account // whose uid must exactly match the key ($userId)
".read": "$user_id === auth.uid",
".write": "$user_id === auth.uid"
}
}
}
}
基本方法对我来说听起来很合理,尽管我会对规则做一些补充:
/usernames
,因为这意味着任何人都可以通过一次写入擦除所有用户。相反,您可能希望他们能够编写每个单独的用户名,因此在 /usernames/$username
./usernames/$username
.auth !== null
检查。/usernames/$username
节点的值,因此您也希望在安全规则中强制执行它。您可能还可以做更多的事情,但上述步骤肯定是向前迈出的重要一步。