7
我在Firebase控制台中创建了两个用户,他们都有不同的用户名和电子邮件地址。Swift + Firebase安全无法使用
我希望他们能够将他们的得分在线存储在数据库中。这是结构:
AppName
- GameStats
- DBW9WQEs2sQn9CuPTE9t7Q1qWSz2
- Score : 0986
- Li75C2BYW7bQnKqMmrqLAZ67HUy4
- Score : 44131
要访问此值,并保持同步我使用这个:
let baseRef = FIRDatabase.database().reference(withPath: "GameStats/" + user.uid + "")
let scoreRef = scoreRef.child("Score")
scoreRef.observe(.value, with: { snapshot in
print(snapshot.value)
})
我想测试这两个用户是否可以从其他用户访问其他信息。我改了行,以包括其他user.uid
像这样:
let baseRef = FIRDatabase.database().reference(withPath: "GameStats/Li75C2BYW7bQnKqMmrqLAZ67HUy4")
// Logged in User: DBW9WQEs2sQn9CuPTE9t7Q1qWSz2
,由于某种原因它输出这样的:
Optional(44131)
如果我更改了数据库中的值,它会自动的值更新到一个我放。
这是错误的用户,由于某种原因它能够访问它。
这是我的规则:
{
"rules": {
".read": "auth != null",
".write": "auth != null",
"GameStats": {
"$user_id": {
".write": "auth != null && auth.uid === $user_id && auth.provider === 'password'",
".read": "auth != null && auth.uid === $user_id && auth.provider === 'password'"
}
}
}
}
为什么应用程序,允许一个用户读取其他用户的数据以及如何限制访问,使用户只能访问自己的用户ID下的数据?
作为@M_G建议,我从父母和.read
拿出.write
。所以我的规则现在:
{
"rules": {
// ".read": "auth != null",
// ".write": "auth != null",
"GameStats": {
"$user_id": {
".write": "auth != null && auth.uid === $user_id && auth.provider === 'password'",
".read": "auth != null && auth.uid === $user_id && auth.provider === 'password'"
}
}
}
}
我现在得到这样的输出:
[FirebaseDatabase] setValue: or removeValue: at /GameStats/DBW9WQEs2sQn9CuPTE9t7Q1qWSz2 failed: permission_denied - This is for the correct user too. I get this error if wrong user also.
您是否尝试删除一般'.write'规则? –
@M_G你的意思是:“.write”:“auth!= null”?我虽然这些规则只是指父母。因此规范的儿童规则 – JamesG
我会试一试,因为GameStats写规则似乎是正确的。 –