How to set Firebase database rules for group messaging app












-1















I have set Firebase database rules for a group messaging app. But now even when an authenticated user tries to send a message it doesn't get displayed.



I am working on a group messaging app. The code can be found at the following link. When creating the app I was using default rules for the Firebase database. But now I am trying set rules so that all the uid available in the members node can see or send messages to that particular group. Below are the latest set of rules that I have created. Below is my detailed database structure after a plan is created. Please note that I am making use of Firebase Phone Authentication. For this reason I am creating a users node which keeps track of uid against every registered number. When a user selects members to be added to the group I check those numbers against their respective uid's and then add those uid's in the member's node.



{
"rules": {
"plan":{
"$planID":{
"messages":{
".read":"data.parent().child('members').child(auth.uid).exists()",
".write":"data.parent().child('members').child(auth.uid).exists()"
}
}
}
}
}


Data Structure



I expected authenticated users who have been added to the members node to be able to read and send messages but instead even the registered users themselves cannot create a new group or read the messages after the above rules have been published.










share|improve this question

























  • Please include the code you are using to test this and your database structure as text.

    – André Kool
    Jan 1 at 23:01











  • @AndréKool I have added a link to the github repository of the project

    – RCR
    Jan 2 at 6:23
















-1















I have set Firebase database rules for a group messaging app. But now even when an authenticated user tries to send a message it doesn't get displayed.



I am working on a group messaging app. The code can be found at the following link. When creating the app I was using default rules for the Firebase database. But now I am trying set rules so that all the uid available in the members node can see or send messages to that particular group. Below are the latest set of rules that I have created. Below is my detailed database structure after a plan is created. Please note that I am making use of Firebase Phone Authentication. For this reason I am creating a users node which keeps track of uid against every registered number. When a user selects members to be added to the group I check those numbers against their respective uid's and then add those uid's in the member's node.



{
"rules": {
"plan":{
"$planID":{
"messages":{
".read":"data.parent().child('members').child(auth.uid).exists()",
".write":"data.parent().child('members').child(auth.uid).exists()"
}
}
}
}
}


Data Structure



I expected authenticated users who have been added to the members node to be able to read and send messages but instead even the registered users themselves cannot create a new group or read the messages after the above rules have been published.










share|improve this question

























  • Please include the code you are using to test this and your database structure as text.

    – André Kool
    Jan 1 at 23:01











  • @AndréKool I have added a link to the github repository of the project

    – RCR
    Jan 2 at 6:23














-1












-1








-1








I have set Firebase database rules for a group messaging app. But now even when an authenticated user tries to send a message it doesn't get displayed.



I am working on a group messaging app. The code can be found at the following link. When creating the app I was using default rules for the Firebase database. But now I am trying set rules so that all the uid available in the members node can see or send messages to that particular group. Below are the latest set of rules that I have created. Below is my detailed database structure after a plan is created. Please note that I am making use of Firebase Phone Authentication. For this reason I am creating a users node which keeps track of uid against every registered number. When a user selects members to be added to the group I check those numbers against their respective uid's and then add those uid's in the member's node.



{
"rules": {
"plan":{
"$planID":{
"messages":{
".read":"data.parent().child('members').child(auth.uid).exists()",
".write":"data.parent().child('members').child(auth.uid).exists()"
}
}
}
}
}


Data Structure



I expected authenticated users who have been added to the members node to be able to read and send messages but instead even the registered users themselves cannot create a new group or read the messages after the above rules have been published.










share|improve this question
















I have set Firebase database rules for a group messaging app. But now even when an authenticated user tries to send a message it doesn't get displayed.



I am working on a group messaging app. The code can be found at the following link. When creating the app I was using default rules for the Firebase database. But now I am trying set rules so that all the uid available in the members node can see or send messages to that particular group. Below are the latest set of rules that I have created. Below is my detailed database structure after a plan is created. Please note that I am making use of Firebase Phone Authentication. For this reason I am creating a users node which keeps track of uid against every registered number. When a user selects members to be added to the group I check those numbers against their respective uid's and then add those uid's in the member's node.



{
"rules": {
"plan":{
"$planID":{
"messages":{
".read":"data.parent().child('members').child(auth.uid).exists()",
".write":"data.parent().child('members').child(auth.uid).exists()"
}
}
}
}
}


Data Structure



I expected authenticated users who have been added to the members node to be able to read and send messages but instead even the registered users themselves cannot create a new group or read the messages after the above rules have been published.







firebase firebase-realtime-database firebase-security-rules






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 6:19







RCR

















asked Jan 1 at 22:02









RCRRCR

14




14













  • Please include the code you are using to test this and your database structure as text.

    – André Kool
    Jan 1 at 23:01











  • @AndréKool I have added a link to the github repository of the project

    – RCR
    Jan 2 at 6:23



















  • Please include the code you are using to test this and your database structure as text.

    – André Kool
    Jan 1 at 23:01











  • @AndréKool I have added a link to the github repository of the project

    – RCR
    Jan 2 at 6:23

















Please include the code you are using to test this and your database structure as text.

– André Kool
Jan 1 at 23:01





Please include the code you are using to test this and your database structure as text.

– André Kool
Jan 1 at 23:01













@AndréKool I have added a link to the github repository of the project

– RCR
Jan 2 at 6:23





@AndréKool I have added a link to the github repository of the project

– RCR
Jan 2 at 6:23












1 Answer
1






active

oldest

votes


















0
















  1. When defining every sub-node as Integers, starting from zero (as you did in members), Firebase automatically interprets the whole node as it if was an Array of Objects, and in such case you cannot treat them as if they were sub-nodes at all (by calling child(auth.uid)).



    Quote from the link:




    In particular, if all of the keys are integers, and more than half of the keys between 0 and the maximum key in the object have non-empty values, then Firebase will render it as an array. This latter part is important to keep in mind.




    Since this is the case in your Data Structure, all of members' sub-nodes are no longer considered nodes. They are array items, and therefore are not children of members.



  2. Even if it weren't the case, and they were all sub-nodes - you seem to call child(auth.uid) as if auth.uid was an Integer: Your "sub-nodes" are all Integers, while auth.uid is defined String.







share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999283%2fhow-to-set-firebase-database-rules-for-group-messaging-app%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0
















    1. When defining every sub-node as Integers, starting from zero (as you did in members), Firebase automatically interprets the whole node as it if was an Array of Objects, and in such case you cannot treat them as if they were sub-nodes at all (by calling child(auth.uid)).



      Quote from the link:




      In particular, if all of the keys are integers, and more than half of the keys between 0 and the maximum key in the object have non-empty values, then Firebase will render it as an array. This latter part is important to keep in mind.




      Since this is the case in your Data Structure, all of members' sub-nodes are no longer considered nodes. They are array items, and therefore are not children of members.



    2. Even if it weren't the case, and they were all sub-nodes - you seem to call child(auth.uid) as if auth.uid was an Integer: Your "sub-nodes" are all Integers, while auth.uid is defined String.







    share|improve this answer






























      0
















      1. When defining every sub-node as Integers, starting from zero (as you did in members), Firebase automatically interprets the whole node as it if was an Array of Objects, and in such case you cannot treat them as if they were sub-nodes at all (by calling child(auth.uid)).



        Quote from the link:




        In particular, if all of the keys are integers, and more than half of the keys between 0 and the maximum key in the object have non-empty values, then Firebase will render it as an array. This latter part is important to keep in mind.




        Since this is the case in your Data Structure, all of members' sub-nodes are no longer considered nodes. They are array items, and therefore are not children of members.



      2. Even if it weren't the case, and they were all sub-nodes - you seem to call child(auth.uid) as if auth.uid was an Integer: Your "sub-nodes" are all Integers, while auth.uid is defined String.







      share|improve this answer




























        0












        0








        0









        1. When defining every sub-node as Integers, starting from zero (as you did in members), Firebase automatically interprets the whole node as it if was an Array of Objects, and in such case you cannot treat them as if they were sub-nodes at all (by calling child(auth.uid)).



          Quote from the link:




          In particular, if all of the keys are integers, and more than half of the keys between 0 and the maximum key in the object have non-empty values, then Firebase will render it as an array. This latter part is important to keep in mind.




          Since this is the case in your Data Structure, all of members' sub-nodes are no longer considered nodes. They are array items, and therefore are not children of members.



        2. Even if it weren't the case, and they were all sub-nodes - you seem to call child(auth.uid) as if auth.uid was an Integer: Your "sub-nodes" are all Integers, while auth.uid is defined String.







        share|improve this answer

















        1. When defining every sub-node as Integers, starting from zero (as you did in members), Firebase automatically interprets the whole node as it if was an Array of Objects, and in such case you cannot treat them as if they were sub-nodes at all (by calling child(auth.uid)).



          Quote from the link:




          In particular, if all of the keys are integers, and more than half of the keys between 0 and the maximum key in the object have non-empty values, then Firebase will render it as an array. This latter part is important to keep in mind.




          Since this is the case in your Data Structure, all of members' sub-nodes are no longer considered nodes. They are array items, and therefore are not children of members.



        2. Even if it weren't the case, and they were all sub-nodes - you seem to call child(auth.uid) as if auth.uid was an Integer: Your "sub-nodes" are all Integers, while auth.uid is defined String.








        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 2 at 4:12

























        answered Jan 2 at 3:49









        BarackosBarackos

        540315




        540315
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999283%2fhow-to-set-firebase-database-rules-for-group-messaging-app%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            How to fix TextFormField cause rebuild widget in Flutter