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

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

问题描述

我的代码比较新,而且有麻烦。

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

  app.userid = app.user.uid 

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

var useridRef = userRef.child(app.userid);
$ b useridRef.set({
locations:,
theme:,
colorScheme:,
food:
});

但是,我不断收到错误:


FIREBASE警告:设置在/ users /(GoogleID)失败:permission_denied
2016-05-23 22:52:42.707 firebase.js:227未被捕获(承诺)错误:PERMISSION_DENIED:权限被拒绝(...)

当我试图查看它时,会谈到Firebase规则,这似乎是在我还没有学习的语言(或只是在我的头上)。有人可以解释是什么造成这个问题?我以为是我要求它存储电子邮件和用户显示名称,你只是不允许这样做,但当我把它们拿出来,我仍然有同样的问题。有没有一种方法可以避免这种错误而不设定规则,或者是规则的东西,我可以教自己如何在一天写字,或者我刚刚离开我的联赛?



感谢您的帮助!

/console.firebase.google.com/rel =noreferrer>新的Firebase控制台只能由经过验证的用户读取/写入:

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

请参阅适用于Firebase数据库安全规则的快速入门指南



用户从你的代码中,数据库拒绝你访问数据。要解决这个问题,你需要允许未经身份验证的数据库访问,或者在访问数据库之前登录用户。



允许对数据库进行未经授权的访问



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

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




$ b $这使得你的新数据库可读并由大家写作。确保在您投入生产之前再次保护您的数据库,否则有人可能会开始滥用它。



在访问数据库之前登录用户



对于(稍微)更耗时但更安全的解决方案,请调用 signIn ... 一个href =https://firebase.google.com/docs/auth/web/start =noreferrer> Firebase身份验证,以确保用户在访问数据库之前登录。最简单的方法是使用匿名身份验证

$ $ $ $ $ $ $ $ $ $ $ $ $ firebase.auth()。signInAnonymously()。catch(function(error){
/ /处理错误
var errorCode = error.code;
var errorMessage = error.message;
// ...
});

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

  firebase.auth()。onAuthStateChanged(function(user){
if(user){
//用户登录。
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 {
//用户退出
// ...
}
// ...
});


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

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 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(…)

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?

Thanks for any help!

解决方案

By default the database in a project in the new Firebase Console is only readable/writeable by authenticated users:

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

See the quickstart for the Firebase Database security rules.

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.

Allow unauthenticated access to your 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
  }
}

This makes your new database readable and writeable by everyone. Be certain to secure your database again before you go into production, otherwise somebody is likely to start abusing it.

Sign in the user before accessing the database

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天全站免登陆