【问题标题】:Firebase user PERMISSION_DENIED: Permission deniedFirebase 用户 PERMISSION_DENIED:权限被拒绝
【发布时间】:2017-04-12 09:47:46
【问题描述】:

我正在使用 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;
            });
          });
        },

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

在屏幕截图下方显示

【问题讨论】:

  • 基于什么你认为用户有写权限?

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


【解决方案1】:

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

  1. 您将配置文件数据写入 当前用户:firebase.database(secondaryApp).ref().child('users')
  2. 在写入完成之前不要退出用户

在代码中:

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;
          });
      });
    },

【讨论】:

  • 谢谢。这样更好。
【解决方案2】:
  1. 删除secondaryApp.auth().signOut();
  2. 更新此return firebase.database(secondaryApp).ref().child('users')

    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;
       });
    });
    },
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    • 2020-02-15
    • 2017-12-26
    相关资源
    最近更新 更多