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寫規則似乎是正確的。 –