Firebase 权限被拒绝 [英] Firebase Permission Denied

查看:29
本文介绍了Firebase 权限被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编码比较陌生,遇到了麻烦.

I'm relatively new to coding and am having trouble.

我有这个代码可以将数据发送到 firebase

I have this code to send data to firebase

app.userid = app.user.uid

var userRef = app.dataInfo.child(app.users);

var useridRef = userRef.child(app.userid);

useridRef.set({
  locations: "",
  theme: "",
  colorScheme: "",
  food: ""
});

但是,我不断收到错误消息:

However, I keep getting the error:

FIREBASE 警告:设置在/users/(GoogleID) 失败:permission_denied2016-05-23 22:52:42.707 firebase.js:227 Uncaught (in promise) 错误:PERMISSION_DENIED:权限被拒绝(…)

FIREBASE WARNING: set at /users/(GoogleID) failed: permission_denied 2016-05-23 22:52:42.707 firebase.js:227 Uncaught (in promise) Error: PERMISSION_DENIED: Permission denied(…)

当我尝试查找它时,它谈到了 Firebase 的规则,这似乎是一种我还没有学过的语言(或者它只是让我无法理解).有人可以解释导致问题的原因吗?我以为是我要求它存储电子邮件和用户显示名称,而您不允许这样做,但是当我将它们取出时,我仍然遇到同样的问题.有没有办法在不设置规则的情况下避免这个错误,或者规则是我可以在一天内教自己如何写的东西,或者我只是离开了我的联盟?

When I try to look this up it talks about rules for Firebase, which seems to be in a language that I haven't learned yet (or it is just going over my head). Can someone explain what is causing the issue? I thought it was that I was asking for it to store email and user display name and you just weren't allowed to do this, but when I took those out I still had the same problem. Is there a way to avoid this error without setting the rules, or are rules something I can teach myself how to write in a day, or am I just way out of my league?

感谢您的帮助!

推荐答案

默认项目中的数据库Firebase 控制台只能由管理用户读取/写入(例如在 Cloud Functions 或使用 Admin SDK 的进程中).除非您更改服务器端安全规则,否则常规客户端 SDK 的用户无法访问数据库.

By default the database in a project in the Firebase Console is only readable/writeable by administrative users (e.g. in Cloud Functions, or processes that use an Admin SDK). Users of the regular client-side SDKs can't access the database, unless you change the server-side security rules.

您可以更改规则,以便只有经过身份验证的用户才能读取/写入数据库:

You can change the rules so that the database is only readable/writeable by authenticated users:

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

请参阅快速入门以了解 Firebase 数据库安全规则.

但由于您没有通过代码登录用户,因此数据库拒绝您访问数据.要解决这个问题,您需要允许未经身份验证的数据库访问,或者在访问数据库之前登录用户.

But since you're not signing the user in from your code, the database denies you access to the data. To solve that you will either need to allow unauthenticated access to your database, or sign in the user before accessing the database.

目前最简单的解决方法(直到教程更新)是进入您项目的控制台中的数据库"面板,选择规则"选项卡并用以下规则替换内容:

The simplest workaround for the moment (until the tutorial gets updated) is to go into the Database panel in the console for you project, select the Rules tab and replace the contents with these rules:

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

这使得知道数据库 URL 的任何人都可以读取和写入您的新数据库.在投入生产之前,请务必再次保护您的数据库,否则有人可能会开始滥用它.

This makes your new database readable and writeable by anyone who knows the database's URL. Be sure to secure your database again before you go into production, otherwise somebody is likely to start abusing it.

要获得(稍微)更耗时但更安全的解决方案,请调用 Firebase 身份验证 以确保用户在访问数据库之前已登录.最简单的方法是使用匿名身份验证:

For a (slightly) more time-consuming, but more secure, solution, call one of the signIn... methods of Firebase Authentication to ensure the user is signed in before accessing the database. The simplest way to do this is using anonymous authentication:

firebase.auth().signInAnonymously().catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  // ...
});

然后在检测到登录时附加您的听众

And then attach your listeners when the sign-in is detected

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
    var isAnonymous = user.isAnonymous;
    var uid = user.uid;
    var userRef = app.dataInfo.child(app.users);
    
    var useridRef = userRef.child(app.userid);
    
    useridRef.set({
      locations: "",
      theme: "",
      colorScheme: "",
      food: ""
    });

  } else {
    // User is signed out.
    // ...
  }
  // ...
});

这篇关于Firebase 权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆