Firebase以管理员身份验证

Pat*_*ivo 35 firebase firebase-security

有没有一种方法,我可以用火力地堡认证为完全读一个火力点的管理员/写访问它(已经有保护它的部分安全规则),或将我必须写一个安全规则,不知怎的让我进入完整的firebase,例如通过提供某个密码/密钥.

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

Kyl*_*yle 50

安德鲁的回答只有在您在客户端代码之外进行身份验证时才会起作用(否则您不应该使用它MY_SECRET).由于很多人(比如我自己)使用Firebase来避免服务器代码,因此这是另一个答案.

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

然后你的规则看起来像这样(假设你的auth.id匹配你的$ user键):

{
  "rules": {
    "someObject": {
      ".write": "root.child('Users').child(auth.uid).child('isAdmin').val() == true"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 您如何相信只有管理员可以将isAdmin设置为true? (7认同)

And*_*Lee 17

就在这里.您只需使用Firebase机密而不是身份验证令牌进行身份验证.即.

firebaseRef.auth(MY_SECRET);
Run Code Online (Sandbox Code Playgroud)

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

  • `auth()`现在被删除了.请参阅Matt的答案http://stackoverflow.com/a/27413337/978501 (8认同)

小智 15

根据文档,您需要使用firebaseRef.authWithCustomToken.

例;

firebaseRef.authWithCustomToken(FIREBASE_SECRET, function(error, authData) {
  if(!error) {
    // You are authenticated
  }
});
Run Code Online (Sandbox Code Playgroud)

firebaseRef.auth现已弃用,请firebaseRef.authWithCustomToken改用.


Bon*_*usK 5

有关“最新”文档,请参阅此内容。

authWithCustomToken 现在是 signInWithCustomToken(firebase 版本 3.x)

文档中的示例:

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);
    }
});
Run Code Online (Sandbox Code Playgroud)