Firebase用户PERMISSION_DENIED:权限被拒绝

时间:2017-04-12 09:47:46

标签: javascript firebase firebase-realtime-database firebase-authentication

我正在使用firebase身份验证,我引用此link

"users": {
        ".read": "auth != null && root.child('admins').child(auth.uid).val() == true",
        ".write": "auth != null && (root.child('admins').child(auth.uid).val() == true)",
        "$uid": {
          ".read": "auth != null && (auth.uid == $uid || root.child('admins').child(auth.uid).val() == true)",
          ".write": "auth != null && (auth.uid == $uid || root.child('admins').child(auth.uid).val() == true)"
        }
      },

这是我的firebase用户表规则。

我的services.js

signupUser: function(newUser) {
          var secondaryApp = firebase.initializeApp(FirebaseAppConfig, "Secondary");
          var usersRef = firebase.database().ref().child('users');
          return secondaryApp.auth()
          .createUserWithEmailAndPassword(newUser.email, newUser.password)
          .then(function(firebaseUser) {
            secondaryApp.auth().signOut();

            var newUserWithoutPwd = _.extend({}, newUser);
            delete newUserWithoutPwd.password;
            return usersRef
            .child(firebaseUser.uid)
            .set(newUserWithoutPwd)
            .then(function() {
              return newUserWithoutPwd;
            });
          });
        },

认证成功。但用户表显示权限被拒绝错误。

显示以下屏幕截图

enter image description here

2 个答案:

答案 0 :(得分:3)

我的猜测是您希望新创建的用户将其用户配置文件写入数据库。在这种情况下,重要的是:

  1. 您将个人资料数据写为当前用户:firebase.database(secondaryApp).ref().child('users')
  2. 在写完成之前,您不会退出用户
  3. 在代码中:

    signupUser: function(newUser) {
          var secondaryApp = firebase.initializeApp(FirebaseAppConfig, "Secondary");
          var usersRef = secondaryApp.database().ref().child('users');
          return secondaryApp.auth()
          .createUserWithEmailAndPassword(newUser.email, newUser.password)
          .then(function(firebaseUser) {
            var newUserWithoutPwd = _.extend({}, newUser);
            delete newUserWithoutPwd.password;
            return usersRef
              .child(firebaseUser.uid)
              .set(newUserWithoutPwd)
              .then(function() {
                secondaryApp.auth().signOut();
                return newUserWithoutPwd;
              });
          });
        },
    

答案 1 :(得分:1)

  1. 删除return firebase.database(secondaryApp).ref().child('users')
  2. 更新此signupUser: function(newUser) { var secondaryApp = firebase.initializeApp(FirebaseAppConfig, "Secondary"); return secondaryApp.auth() .createUserWithEmailAndPassword(newUserInsert.email, newUserInsert.password) .then(function(firebaseUser) { var newUserWithoutPwd = _.extend({}, newUserInsert); delete newUserWithoutPwd.password; return firebase.database(secondaryApp).ref().child('users') .child(firebaseUser.uid) .set(newUserWithoutPwd) .then(function() { return newUserWithoutPwd; }); }); },

    QString myRequest = "CREATE DATABASE MY_DATABASE;";
    lQueryResult.Exec(myRequest);
    if(!lQueryResult.GetMexec())
    {
        qDebug() << "Error in the request";
        return false;
    }
    else
    {
        qDebug() << "The request is OK";
    }