angularfire - Firebase read are returning permission_denied when I'm logged with password -
i'm getting permission_denied when tried read user-mappings data "authentication password", i'm able read if i'm logged linkedin. whould know i'm doing wrong?
this structure of data , rules:
//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" } } }
basically, i'm trying access "user-mappings/linkedin/$linkedin_uid" data when i'm logged email , password.
my code is:
//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 this, gor permission_denied error });
it's still not clear, best guess you're trying load data before user authenticated. if case, easiest see what's going on adding logging:
//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 this, got permission_denied error });
if guess correct, output in order:
starting auth getting user mappings user mappings gotten authenticated
so loading of user mappings started before user authentication has completed. happens because user authentication happens asynchronously.
to solve problem should move code requires user authenticated promise resolves when user authentication has completed:
//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 this, got permission_denied error }); //some code here }).catch(function(error) { //some code here });
Comments
Post a Comment