Firebase数据库的权限错误

时间:2016-06-04 12:57:22

标签: javascript firebase firebase-authentication

我正在创建一个注册Web表单来使用UID创建用户和存储信息。当我尝试创建用户数据时,我收到FIREBASE WARNING: set at /User/4FzbgqXVX8NlbB0pBkDdJeZ9HHe2 failed: permission_denied

我的firebase数据库规则如下。希望确保除了管理员和用户之外,所有用户都无法使用此数据

{
  "rules": {
    "users": {
      "$uid": {
        ".read": "$uid === auth.uid",
        ".write": "$uid === auth.uid"
      }
    }
  }
}

创建用户的功能和写入数据如下

<script>
function createUser() {
var Result = "true";
var textUser = document.getElementById('userName').value;
var textPassword = document.getElementById('userPassword').value;
var textAccountID = document.getElementById('accountRef').value;
var textDateCreated = document.getElementById('dateCreated').value;
var textDisplayName = document.getElementById('displayName').value;
var UID;

firebase.auth().createUserWithEmailAndPassword(textUser, textPassword).then(function(user){
  console.log('uid',user.uid);//Here if you want you can sign in the user
  UID = user.uid;

  writeUserData(UID, textDisplayName, textAccountID, textDateCreated);

}).catch(function(error) {
    //Handle error
  var errorCode = error.code;
  var errorMessage = error.message;
  return Result = "false";
});

return Result;

}

function writeUserData(userId, displayName, accountID, dateCreated) {
  firebase.database().ref('User/' + userId).set({
  userId:{
    AccountID: accountID,
    Created: dateCreated,
    Name: displayName}
  });

    firebase.auth().signOut().then(function() {
      // Sign-out successful.
    }, function(error) {
      // An error happened.
    });
}


</script>

有没有人知道如何协商上述问题?

更新

我已将请求的节点更改为像这样的用户

firebase.database().ref('users/' + userId).set({
firebase.database().ref('/users/' + userId).set({
firebase.database().ref('/users' + userId).set({

我的规则看起来像这样

{
  "rules": {
    "users": {
      "$uid": {
        ".read": "$uid === auth.uid",
        ".write": "$uid === auth.uid"
      }
    }
  }
}

但我仍然收到与权限相关的以下错误消息

FIREBASE WARNING: set at /users/bgzolYFgEgY29WYsNEI0vyULXp13 failed: permission_denied 

我还尝试将规则更改为默认规则,如下所示 ,但我仍然得到许可被拒绝

// These rules require authentication
{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}

1 个答案:

答案 0 :(得分:0)

我花了一些时间来纠正上述问题,但我很高兴我已经完成了它。对于那些也遇到类似问题的人来说,答案是

首先,我错误地将信息传递给数据库。我提供以下内容:

  userId:{
    AccountID: accountID,
    Created: dateCreated,
    Name: displayName}

我现在提供这个:

AccountID: accountID,
Created: dateCreated,
Name: displayName

我认为上述情况导致我的许可错误

这适用于某些电话但不是全部......

其次我认为firebase调用可能是异步任务,因此在set命令之后直接调用signOut()可能会导致我的一些间歇性结果。我正确地添加了所有的promise并调用signOut()并从这里写入数据函数。

现在这一切似乎都很顺利

<script>
function createUser() {
var Result = "true";
var textUser = document.getElementById('userName').value;
var textPassword = document.getElementById('userPassword').value;
var textAccountID = document.getElementById('accountRef').value;
var textDateCreated = document.getElementById('dateCreated').value;
var textDisplayName = document.getElementById('displayName').value;


firebase.auth().createUserWithEmailAndPassword(textUser, textPassword).then(function(user){
  console.log('uid',user.uid);//Here if you want you can sign in the user
  console.log('success','user added in createUser');  
  writeUserData(user.uid, textDisplayName, textAccountID, textDateCreated);

}).catch(function(error) {
    console.log('error','Error when creating user');
    //Handle error
  var errorCode = error.code;
  var errorMessage = error.message;
  return Result = "false";
});

return Result;

}

function writeUserData(userId, displayName, accountID, dateCreated) {
  firebase.database().ref('users/' + userId).set({
    AccountID: accountID,
    Created: dateCreated,
    Name: displayName
  }).then(function() {
  console.log('success','Added record in writeUserData');
        signUserOut();

  })
  .catch(function(error) {
  console.log('error','Error in writeUserData');
    //Handle error
  var errorCode = error.code;
  var errorMessage = error.message;
  return Result = "false";

    signUserOut();

});


}

function signUserOut(){
        firebase.auth().signOut().then(function() {
        console.log('success','signed user out');
      // Sign-out successful.
    }, function(error) {
    console.log('error','error signing out user');
        var errorCode = error.code;
        var errorMessage = error.message;
      // An error happened.
    });

}


</script>

我的数据库规则如下所示

{
  "rules": {
    "users": {
      "$user_id": {
        // grants write access to the owner of this user account
        // whose uid must exactly match the key ($user_id)
        ".read": "$user_id === auth.uid",
        ".write": "$user_id === auth.uid"
      }
    }
  }
}

感谢所有帮助