2016-10-04 71 views
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. 
+0

您是否嘗試刪除一般'.write'規則? –

+0

@M_G你的意思是:「.write」:「auth!= null」?我雖然這些規則只是指父母。因此規範的兒童規則 – JamesG

+0

我會試一試,因爲GameStats寫規則似乎是正確的。 –

回答

3

火力地堡文件是錯誤的(現在)。在Firebase控制檯中,打開規則模擬器。目前沒有「密碼」選項,我認爲它是一個錯誤。

如果您沒有使用多重身份驗證或多重身份驗證在您的項目中無關緊要,請不要在您的規則中使用提供程序。否則,您可以測試此規則進行密碼驗證:

".write": "auth != null && auth.uid === $user_id && auth.token.firebase.identities.email !== null"