2013-07-12 130 views
31

有没有一种方法可以作为Firebase的管理员对Firebase进行身份验证,以便对其进行完整的读/写访问(已具有保护其部分内容的安全规则),还是必须编写安全规则以某种方式允许我访问完整的Firebase,例如通过提供特定的密码/密钥。Firebase以管理员身份进行身份验证

有没有这样做的标准或建议的方式?

回答

1

See this for the 'latest' documentation.

authWithCustomToken现在是signInWithCustomToken(火力版本3)

实施例从文档:

firebase.auth().signInWithCustomToken(token).catch(function(error) { 
    // Handle Errors here. 
    var errorCode = error.code; 
    var errorMessage = error.message; 
    if (errorCode === 'auth/invalid-custom-token') { 
     alert('The token you provided is not valid.'); 
    } else { 
     console.error(error); 
    } 
}); 
+0

嗨,我看到这是连接到firebase的常规auth java,ios js等)。我如何在Python中执行此操作(我的意思是我如何以管理员身份登录)。我怎样才能确保谁使用我的管理程序是一个真正的管理员? – Tomer

17

是的。您只需使用Firebase秘密进行身份验证,而不需要身份验证令牌。即。

firebaseRef.auth(MY_SECRET); 

您可以在Forge的Authentication部分找到Secret。

+8

'AUTH()'现在depricated。请参阅Matt的回答http://stackoverflow.com/a/27413337/978501 – Sridhar

+1

我使用了ref.authWithCustomToken(),但返回的有效内容具有空属性:auth,expires,token,uid。除了设置为'custom'的'provider'属性外。这是什么原因? – nodebase

+0

@nodebase响应(authData)是Object {auth:null,expires:null,token:“”,uid:null,provider:“custom”}“但它违背规则'”.read“:”auth.admin == true“' –

46

Andrew的答案仅适用于您在客户端代码之外进行身份验证(否则您显然不应使用MY_SECRET)。由于像我这样的许多人使用Firebase来避免服务器代码,因此这里有一个替代答案。

在大多数firebase应用程序中,除了简单登录“auth”对象(仅存储电子邮件和密码)外,您可能还有一个“用户”对象。您可以在“用户”对象中为每个$用户添加一个“isAdmin”或“isModerator”(或任何您想要的)。

然后你的规则是这样的(假设你的auth.id相匹配的$用户密钥):

{ 
    "rules": { 
    "someObject": { 
     ".write": "root.child('Users').child(auth.uid).child('isAdmin').val() == true" 
    } 
    } 
} 
+1

我刚刚开始使用Firebase,我非常喜欢你的建议,但不应该是'child(auth.uid)'?只是好奇......就像我说的,仍在学习:) –

+0

是的@BenjaminSolum你是对的,auth.uid是新的auth.id。 Firebase完全移除了auth.id,该auth.id仅对提供商唯一,并用全球唯一的ID替换。我在答案中更新了它,希望它能作为一个整体。 – Kyle

+0

有关使用相等运算符的任何建议,即'.val()== true'或'.val()=== true'? –

15

As per the documentation,你需要使用firebaseRef.authWithCustomToken

示例;

firebaseRef.authWithCustomToken(FIREBASE_SECRET, function(error, authData) { 
    if(!error) { 
    // You are authenticated 
    } 
}); 

firebaseRef.auth现在已过时,使用firebaseRef.authWithCustomToken代替。

+0

响应是(authData)Object {auth:null,expires:null,token :“”,uid:null,provider:“custom”}'但它违背规则'“.read”:“auth.admin == true”' –