Client does not have permission to access the desired Firebase data - firebase

I have a page that is calling addCheckin() method which is inside a controller. In the controller, I am trying to create a reference as follows:
var ref = firebase.database().ref("users/" + $scope.whichuser + "/meetings/" +$scope.whichmeeting + "/checkins");
$scope.whichuser and $scope.whichmeeting are the $routeParams that I am passing from another route.
Here's my checkin controller-
myApp.controller("CheckinsController",
['$scope','$rootScope','$firebaseArray','$routeParams','$firebaseObject',
function($scope,$rootScope,$firebaseArray,$routeParams,$firebaseObject){
$scope.whichuser = $routeParams.uid;
$scope.whichmeeting = $routeParams.mid;
var ref = firebase.database().ref("users/" + $scope.whichuser + "/meetings/" +$scope.whichmeeting + "/checkins");
$scope.addCheckin = function(){
var checkinInfo = $firebaseArray(ref);
var data={
firstname:$scope.firstname,
lastname:$scope.lastname,
email:$scope.email,
date:firebase.database.ServerValue.TIMESTAMP
}
checkinInfo.$add(data);
}
}]);/*controller*/
There are two errors that I am getting here-
Error 1:
Error: permission_denied at /users/Vp2P1MqKm7ckXqV2Uy3OzTnn6bB3/meetings: Client doesn't have permission to access the desired data.
Error 2:
Error: permission_denied at /users/Vp2P1MqKm7ckXqV2Uy3OzTnn6bB3/meetings/-KT5tqMYKXsFssmcRLm6/checkins: Client doesn't have permission to access the desired data.
And this is what I am trying to achieve:
Here's my JSON tree of the database:
{
"users" : {
"43ZU72ndoKb9LSEjBODKXe3nrou1" : {
"email" : "roy#123.com",
"firstname" : "Roy",
"lastname" : "Marshal",
"regUser" : "43ZU72ndoKb9LSEjBODKXe3nrou1"
},
"BHcYb40FC8YlCKL54ngyMhxPx2q1" : {
"date" : 1475574109981,
"email" : "sky#on.com",
"firstname" : "Sky",
"lastname" : "Thakur",
"meetings" : {
"-KTE3_45wXBqd-_yR1xv" : {
"date" : 1475574125198,
"name" : "ARET"
}
},
"regUser" : "BHcYb40FC8YlCKL54ngyMhxPx2q1"
},
"Vp2P1MqKm7ckXqV2Uy3OzTnn6bB3" : {
"email" : "jon#me.com",
"firstname" : "John",
"lastname" : "Cena",
"meetings" : {
"-KT5tqMYKXsFssmcRLm6" : {
"date" : 1475437094667,
"name" : "hourly"
},
"-KTAvH-ZkWyxrJrK_SDb" : {
"date" : 1475521356544,
"name" : "Hourly"
},
"-KTAwV69txdeT4i_horE" : {
"date" : 1475521676006,
"name" : "asdfg"
},
"-KTAwkH35EyPQZ17DmN9" : {
"date" : 1475521742244,
"name" : "qwerty"
},
"-KTAwsrqe24e8wFfRxpN" : {
"date" : 1475521777422,
"name" : "asfdsf"
},
"-KTAyYPSZLQUtOJ4g8Ak" : {
"date" : 1475522213781,
"name" : "Yearly"
},
"-KTB05PfXZLLk9Of3_Nb" : {
"date" : 1475522881473,
"name" : "Meet"
},
"-KTBG2cP9YLoP4lalN5M" : {
"date" : 1475527064378,
"name" : "JOI"
}
},
"regUser" : "Vp2P1MqKm7ckXqV2Uy3OzTnn6bB3"
}
}
}
My database rules are:
{
"rules": {
".read": true,
".write": true
}
}
I also posted this question before, but didn't get any appropriate solution.

Related

Wiremock matchesJSONPath if null or empty

I'm trying to add a Wiremock stub that matches if the JSON in a request body is either non-existent OR an empty string.
The stub I have at the moment is:
{
"id" : "e331007e-3e6d-4660-b575-b04e774e88c6",
"request" : {
"urlPathPattern" : "/premises/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/bookings/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/non-arrivals",
"method" : "POST",
"bodyPatterns" : [ {
"matchesJsonPath" : "$.[?(#.reason === '' || #.reason == null)]"
} ]
},
"response" : {
"status" : 400,
"jsonBody" : {
"type" : "https://example.net/validation-error",
"title" : "Invalid request parameters",
"code" : 400,
"invalid-params" : [ {
"propertyName" : "reason",
"errorType" : "blank"
} ]
},
"headers" : {
"Content-Type" : "application/problem+json;charset=UTF-8"
}
},
"uuid" : "e331007e-3e6d-4660-b575-b04e774e88c6"
}
It matches is the reason is '', but not if reason is not present. Any ideas?

Get list of items based on user id (from array of user id) firebase Realtime Database

I want to fetch the list of devices based on user id from the array of users list. data base shown below.
{
"devices" : {
"aglow6534" : { // Device ID
"icon" : "switch",
"name" : "AC",
"state" : {
"switch" : true
},
"type" : "RLY",
"users" : {
"rIXU5HucbVMTt3foI3AG7q5wBND3" : true // User ID
}
},
"aglow8728" : { // Device ID
"icon" : "bulb",
"name" : "bulb",
"state" : {
"intensity" : 45,
"switch" : true
},
"type" : "DIMR",
"users" : {
"YBJAlwc0vQSmd1TYe190Of8V57y1" : true, // User ID
"rIXU5HucbVMTt3foI3AG7q5wBND3" : true // User ID
}
}
},
"users" : {
"YBJAlwc0vQSmd1TYe190Of8V57y1" : { // User ID
"age" : 28,
"name" : "vipul garg"
},
"rIXU5HucbVMTt3foI3AG7q5wBND3" : { // User ID
"age" : 17,
"name" : "puneet"
}
}
}
These are my rules, i tried my best to fetch data, please help i almost spend 2 days on it.
{
"rules": {
"devices" : {
".read" : "root.child('users').child(auth.uid).child('devices').hasChild(data.child('/').val())",
}
},
}
Required Result:
Example 1: let User Id: YBJAlwc0vQSmd1TYe190Of8V57y1
"devices" : {
"aglow8728" : {
"icon" : "bulb",
"name" : "bulb",
"state" : {
"intensity" : 45,
"switch" : true
},
"type" : "DIMR",
"users" : {
"YBJAlwc0vQSmd1TYe190Of8V57y1" : true,
"rIXU5HucbVMTt3foI3AG7q5wBND3" : true
}
}
},
Example 2: let User Id: rIXU5HucbVMTt3foI3AG7q5wBND3
"devices" : {
"aglow6534" : {
"icon" : "switch",
"name" : "AC",
"state" : {
"switch" : true
},
"type" : "RLY",
"users" : {
"rIXU5HucbVMTt3foI3AG7q5wBND3" : true
}
},
"aglow8728" : {
"icon" : "bulb",
"name" : "bulb",
"state" : {
"intensity" : 45,
"switch" : true
},
"type" : "DIMR",
"users" : {
"YBJAlwc0vQSmd1TYe190Of8V57y1" : true,
"rIXU5HucbVMTt3foI3AG7q5wBND3" : true
}
}
},
I think you need to fully understand Firebase Realtime Database Rules first and other 8 links in Security & Rules section
Your rules is wrong on syntax. It should be something like this, maybe
{
"rules": {
"devices": {
"$devices_id": {
".read": "true",
".write": "false" // false if want to read only
}
},
"users": {
"$users_id": {
".read": "auth != null && auth.uid == $users_id" // this is for If you wanted to restrict access to this data such that only the logged-in user can see their own data
".write": "auth != null && auth.uid == $users_id"
}
}
}
}

How to orderByChild in nested Child in angularfire?

My firebase database structure is as below. I would like to search with a particular "Info/Email", let's say "abc#abc.com".
{
"-KhWrBcYyMluJbK7QpnK" : {
"Info" : {
"Email" : "xyz#gmail.com"
},
"Settings" : {
"Status" : "Accepted"
}
},
"-KhX0tgQvDtDYqt4XRoL" : {
"Info" : {
"Email" : "abc#abc.com"
},
"Settings" : {
"Status" : "Accepted"
}
},
"-KhX1eo7uFnOxqncDXQ5" : {
"Info" : {
"Email" : "abc#abc.com"
},
"Settings" : {
"Status" : "Pending"
}
}
}
I added a rule too
"Invitation" : {
".indexOn": ["Info/Email","Settings/Status"]
}
My AngularFire code is as follows:
var rootRef = firebase.database().ref().child('Invitation');
var userInvitations = rootRef.child("Info").orderByChild("Email").equalTo("abc#abc.com");
var allInvitations = $firebaseArray(userInvitations);
But I am getting a FIREBASE WARNING: Using an unspecified index. Consider adding ".indexOn": "Email" at /Invitation/Info to your security rules for better performance. and of course I am not receiving any data.
What are the mistakes I made here? Also can I add multiple orderByChild, for example: if I want to find details of the record, which has "Info/Email" equal to "abc#abc.com" and "Settings/Status" equal to "Pending" ?
The Firebase API only allows you to filter children one level deep
So with reference to your data structure:
if it were to be this way,
{
"-KhWrBcYyMluJbK7QpnK" : {
"Email" : "xyz#gmail.com",
"Settings" : {
"Status" : "Accepted"
}
},
"-KhX0tgQvDtDYqt4XRoL" : {
"Email" : "abc#abc.com",
"Settings" : {
"Status" : "Accepted"
}
},
"-KhX1eo7uFnOxqncDXQ5" : {
"Email" : "abc#abc.com",
"Settings" : {
"Status" : "Pending"
}
}
}
You should write your rules this way instead.
"Invitation" : {
"Info" : {
".indexOn": "Email",
},
"Settings" : {
".indexOn": "Status",
}
}
Then you will be able to query the data
var allInvitations = [];
var rootRef = firebase.database().ref().child('Invitation');
var userInvitations = rootRef.orderByChild("Email").equalTo("abc#abc.com");
userInvitations.on('value', snap => {
var invitations = snap.val()
for (prop in invitations ) {
allInvitations.push(invitations[prop ]);
}
console.log(allInvitations);
})

Firebase Retrieving a specific data

"Company" : {
"-KRRqrR3Z3R8u1wRW6dd" : {
"PersonalInfo" : {`
"address" : "bakakeng",
"birthday" : "1992",
"firstName" : "Saoud",
"gender" : "Male",
"midName" : "Ahmed",
"phoneNo" : "12a12a12a",
"surName" : "Al- Maari"
},
"Qualification" : {
"Degree" : "PHD",
"Expreince" : "Doctor"
}
},
"-KRS1Jp2hhAbtLNx9jmq" : {
"PersonalInfo" : {
"address" : "sfsfsf",
"birthday" : "sfsfsfs",
"firstName" : "dfdsf",
"gender" : "sfsf",
"midName" : "sfsf",
"phoneNo" : "123456789",
"surName" : "jfkldsf"
}
}
}
You can order by a nested child as long as that child is at a fixed path.
In JavaScript:
var query = ref.child("Company ").orderByChild("PersonalInfo/phoneNo");
query.on('child_added', function(snapshot) {
console.log(snapshot.val());
});

Firebase querying data limitToLast

I am still working with previous API, however I just came across one little issue. I am trying to query data from Firebase based on the limitToLast(100), however the .val() of snapshot is always null.
Here is sample data structure of my Messaging:
“Messaging” :{
"10153530036627802" : {
"10155174395220402" : {
"LastUpdate" : 1429295238531,
"Messages" : {
"1428338024339" : {
"date" : 1428338024339,
"message" : "Yo",
"sender" : "Uksz"
},
"1428338024488" : {
"date" : 1428338024487,
"message" : "Yo",
"sender" : "Uksz"
},
"1429023827304" : {
"date" : 1429023827303,
"message" : "Fygh",
"sender" : "Uksz"
},
"1429023828469" : {
"date" : 1429023828468,
"message" : "Fygh",
"sender" : "Uksz"
},
"1429295238532" : {
"date" : 1429295238531,
"message" : "Hgh",
"sender" : "Uksz"
}
},
"Name" : "Matt",
"Picture" : "https://scontent.xx.fbcdn.net/xxxx"
},
NEXT_USER_ID
And my code is:
var firebaseRef = new Firebase ("https://xxxx.firebaseio.com/Messages/");
var messageRef = firebaseRef.child(userId).child(recipient).child("Messages");
var messagesRef2 = messageRef.limitToLast(10);
messagesRef2.on("value", function(snapshot){
console.log(snapshot.val())
})
Whenever I am getting snapshot.val(), its value is equal to null. Where did I go wrong?
Thanks,
Uksz

Resources