0

当我尝试使用“使用密码进行身份验证”读取用户映射数据时,我得到了 permission_denied,但是如果我使用我的linkedin 登录,我就可以读取。我想知道我做错了什么?

这是我的数据和规则结构:

//Firebase Data
user-mappings: {
  linkedin: {
    MyLikedinID: {
      user: {
        uid: "simplelogin:1"
      }
    }
  }
}

//Firebase Rules
"user-mappings": {
  "linkedin": {
    "$linkedin_uid": {
      ".read": "auth !== null && (
        (auth.provider === 'linkedin' && auth.id === $linkedin_uid) ||
        (auth.uid === data.child('user/uid').val())
      )",
      ".write": "true"
    }
  }
}

基本上,当我使用电子邮件和密码登录时,我正在尝试访问“user-mappings/linkedin/$linkedin_uid”数据。

我这样做的代码是:

//Login

auth.$authWithPassword(user).then(function(authDataResult) {
  //Some code here
}).catch(function(error) {
  //Some code here
});

//Get user-mappings

var objRef = self.getRef('/user-mappings/linkedin/MyLinkedinID');
var obj = $firebaseObject(objRef);
obj.$loaded().then(function(data) {
  //When I do this, I gor the permission_denied error
});
4

1 回答 1

0

它仍然不完全清楚,但我最好的猜测是您正在尝试在用户通过身份验证之前加载数据。如果是这种情况,通常最容易通过添加一些日志来查看发生了什么:

//Login
console.log('starting auth');
auth.$authWithPassword(user).then(function(authDataResult) {
  console.log('authenticated');
  //Some code here
}).catch(function(error) {
  //Some code here
});

//Get user-mappings

console.log('getting user mappings');
var objRef = self.getRef('/user-mappings/linkedin/MyLinkedinID');
var obj = $firebaseObject(objRef);
obj.$loaded().then(function(data) {
  console.log('user mappings gotten');
  //When I do this, I got the permission_denied error
});

如果我的猜测是正确的,您的输出将按以下顺序排列:

starting auth
getting user mappings
user mappings gotten
authenticated

因此,用户映射的加载在用户身份验证完成之前开始。发生这种情况是因为用户身份验证是异步发生的。

要解决此问题,您应该将任何需要对用户进行身份验证的代码移动到在用户身份验证完成时解决的承诺中:

//Login
console.log('starting auth');
auth.$authWithPassword(user).then(function(authDataResult) {
  console.log('authenticated');

  //Get user-mappings
  console.log('getting user mappings');
  var objRef = self.getRef('/user-mappings/linkedin/MyLinkedinID');
  var obj = $firebaseObject(objRef);
  obj.$loaded().then(function(data) {
    console.log('user mappings gotten');
    //When I do this, I got the permission_denied error
  });

  //Some code here
}).catch(function(error) {
  //Some code here
});
于 2015-08-07T22:40:22.153 回答