2016-10-04 64 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"