firebase 上的用户身份验证,显示受限内容

User authentication on firebase, show restricted content

我正在制作这个带有简单图片库的网站。我正在尝试在其上添加一个简单的用户身份验证,以便只有一个人可以在图库中添加和删除图像。我知道使用 PHP 可能很简单,但我从未使用过它,所以我不知道它对我来说到底有多简单?我发现我可以在 firebase 中进行用户身份验证,所以我想尝试一下。到目前为止我有这个代码:

var ref = new Firebase("https://test.firebaseIO.com");
var authenticate = function(){
    ref.authWithPassword({
        email: $(".email").val(),
        password: $(".password").val()
    }, function(error, authData) {
        if (error) {
            console.log("Something went wrong", error);
        } else {
            console.log("Authenticated with payload", authData);

        }
    });
}

$(".login_button").click(function(){
    authenticate();
});

我正在将 authData 作为控制台日志返回,因此身份验证正在运行。现在我想知道。我怎样才能使用它只向经过身份验证的用户显示内容?或者甚至可以像这样进行身份验证?

感谢您的耐心等待。

虽然这是一个宽泛的问题,详细的答案在 Firebase 网站上,但这里是一个宽泛的答案。

Firebase 有两个部分来保护您的数据(一般来说)

1) Authenticating the user (via email/pw, Facebook integration etc)
2) Once authenticated, controlling what that authenticated user can access via Rules.

规则是您正在寻找的问题答案。

Firebase 数据最初是完全开放的;如果您使用 Firebase 仪表板创建新应用 space,则任何用户(无论是否经过身份验证)都可以访问该数据。

一旦您设置了一组规则,它就会定义什么和谁可以访问相同的数据。

这是一条规则,可确保 read/write 访问权限仅授予已通过身份验证的用户

{
  rules: {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}

这与您的身份验证相结合将只允许经过身份验证的用户访问您的数据;未经身份验证的用户将无法读取或写入它。