当我使用密码登录时,Firebase 读取返回 permission_denied
Firebase read are returning permission_denied when I'm logged with password
当我尝试使用 "authentication with password" 读取用户映射数据时,我得到 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
});
目前还不完全清楚,但我的最佳猜测是您正试图在用户通过身份验证之前加载数据。如果是这种情况,通常最容易通过添加一些日志记录来查看发生了什么:
//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
});
当我尝试使用 "authentication with password" 读取用户映射数据时,我得到 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
});
目前还不完全清楚,但我的最佳猜测是您正试图在用户通过身份验证之前加载数据。如果是这种情况,通常最容易通过添加一些日志记录来查看发生了什么:
//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
});