Firebase is currently experiencing issues with authenticating and removing onDisconnect() functionality

I am encountering issues with implementing onDisconnect().remove() in conjunction with authentication/security rules. Here is what I have set up:

Initially, the user is logged in using auth():

var rootRef = new Firebase(FIREBASE_URL + 'sites/' + FIREBASE_ROOT);
rootRef.auth(user.FIREBASE_TOKEN, loginCallback);

The loginCallback then registers the current user as active and ensures that the user reference is removed on disconnection:

var activeUsers = $firebase(rootRef.child('pages/' + pageId + '/users')).$asArray();
var rawUser = {
  id: user.id,
  displayName: user.displayName
};
activeUsers.$add(rawUser).then(function (userRef) {

  userRef.onDisconnect().remove();

});

The security rules for this specific part of my Firebase database are structured like this:

{
  "rules": {
    "sites": {
      "$siteName": {
        "pages": {
          "$pageId": {
            "users": {
              // only users with firebase access can use this
              ".read": "auth.firebase_user === true"
              "$arrayId": {
                // users can only write their own data
                ".write": "auth.firebase_user === true && auth.id === newData.child('id').val()",
                ".validate": "newData.hasChildren(['id', 'displayName'])"
              }
            }
          }
        }
      }
    }
  }
}

Despite having these security settings and JavaScript code, the user references are not being properly removed from Firebase on disconnection. However, when using these altered security rules instead, the removal functions correctly:

{
  "rules": {
    "sites": {
      "$siteName": {
        "pages": {
          "$pageId": {
            "users": {
              // only users with firebase access can use this
              ".read": "auth.firebase_user === true",
              ".write": "true", 
              "$arrayId": {
                // users can only write their own data
               //".write": "auth.firebase_user === true && auth.id === newData.child('id').val()",
                ".validate": "newData.hasChildren(['id', 'displayName'])"
              }
            }
          }
        }
      }
    }
  }
}

User references are successfully removed on disconnection with this setup.

I have experimented with various security rules and JavaScript implementations, but they all lead to the same issue.

Could there be a potential authentication problem here? Do the user's auth variables reach Firebase in time to remove the user correctly?

In addition, I am utilizing AngularFire for this implementation, which serves as a presence feature to display who else is viewing the current page.

Answer №1

After some investigation, I discovered that the security rules were causing issues with my access to Firebase objects. I made changes to my security rules to address this problem:

{
  "rules": {
    "sites": {
      "$siteName": {
        "pages": {
          "$pageId": {
            "users": {
              ".read": "auth.firebase_user === true",
              ".write": "auth.firebase_user === true",
              "$arrayId": {
                ".validate": "newData.hasChildren(['id', 'displayName']) && newData.child('id').val() === auth.id"
              }
            }
          }
        }
      }
    }
  }
}

Now, Firebase users have read and write permissions, but any data they input must comply with the validation rules.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

AngularJS directive: handling child elements

Is there a way to structure the directive below in order to have access to all the ul elements within the link function? In the code snippet provided, when examining the elm (logged in console), it appears as a comment type and ul are displayed as sibling ...

I am experiencing issues with my asynchronous request in Express

I'm currently in the process of transitioning my requests to async calls to prevent them from blocking. app.js app.use(function(err, req, res, next) { res.locals.message = err.message; res.locals.error = req.app.get("env") === "development" ? er ...

The POST function is executed twice, with the first attempt resulting in a failed API call, but the second attempt is

I am experiencing issues with my subscribe dialog, as it seems to be running the API call twice. The first time it fails, but on the second attempt, it succeeds and inserts the email into the database. This double run is causing errors in my AJAX function, ...

Having trouble getting Vue-Select2 to display properly in Vuejs?

After setting up the vue3-select2-component and following their instructions, I encountered an issue where the component was not displaying in the output on the html page, even though the code for it was present in the source code. For context, I am using ...

Leverage the power of getServerSideProps when working with Dynamic Routes

I have been working on implementing getServerSideProps in my file named [username].js which utilizes dynamic routing. Next.js requires the use of both getStaticPaths() and getStaticProps({ params }) for dynamic routing. However, there seems to be an issu ...

The Facebook bots are unable to crawl our AngularJS application because the JavaScript is not being properly

I have a unique setup where my website is built with AngularJS and Wordpress as a single page application. Depending on the routing of the page, I dynamically define meta tags in the controller. Here's a snippet of my HTML header: <meta property=" ...

Retrieving a single number from Jquery's offset function during scroll events

Seeking the Current Top Position of Web Page Elements In my web page, I have divs with a height of 100%, each containing different content. I am attempting to determine the top position of these divs to manipulate them accordingly. However, when I try to ...

What is the best way to fulfill promises sequentially?

I'm currently facing a challenge with organizing promises in the correct order. I am developing a chat bot for DiscordApp using Node.js and have extensively searched both on this platform and Google. Despite trying Promise.all and an Async function, I ...

Struggling to adjust the size of d3 v6 in a React hooks environment?

I have been attempting to resize my SVG using the width and height from the state: const [svgWidth, setSvgWidth] = useState(350); const [svgHeight, setSvgHeight] = useState(250); useEffect(() => { const svg = d3 .select("#epi-char ...

Issues with rendering of triangles in WebGL

My current project involves creating a webGL application for rendering randomly generated terrains. While the terrain rendering works smoothly, I've encountered an issue with rendering a simple quad to represent water - the triangles of the water are ...

Implementing an AJAX request in jQuery UI AutoComplete feature

I'm currently working on implementing an autocomplete search field that retrieves suggestions from a database through an ajax call. The goal is for the selected item to update the search field accordingly. Unfortunately, I'm running into an issue ...

Working towards ensuring my website is responsive

Hello, I am a CSS beginner currently working as an intern. My task is to make a website's CSS compatible with Internet Explorer, and then make it responsive and scalable. Essentially, the design should retain its appearance when the window size change ...

While the data from Angular $resource can be viewed, it is not accessible in the code

As a newcomer to Angular, I'm facing a frustrating issue that I need help with. My $resource is fetching data from the server in key/value pairs like detail.name and detail.email. While I can access this data using {{detail.name}} in the view, I&apo ...

Integrating Vue with Firestore: A Guide to Binding and Merging Data using Vuefire

My cloud firestore is organized with the following structure: baskets basket 1 basketID userID description products productID productID basket 2 basketID userID descri ...

Delete Entries in MongoDB Collection According to Unique User Pairs

I have a collection of messages stored in MongoDB and I need to keep only the latest 500 records for each pair of users. Users are identified by their sentBy and sentTo attributes. /* 1 */ { "_id" : ObjectId("5f1c1b00c62e9b9aafbe1d6c&quo ...

Tips for creating random CSS values for an element generated with javascript?

Apologies for the blunt title, but I couldn't find a more precise way to phrase it. I recently stumbled upon this fantastic slider tutorial. Before implementing it on my own page, I would like to customize it by replacing the small circles at the bott ...

Extract the chosen document from an input within the Electron application form

Need help with this form <form> <input type="file" name="idp" id="idp" onchange="uploadFiles();"/> </form> Once a user selects an image, I want to move it to a specific folder and save its full name in a variable for database storage. ...

Performing addition and subtraction calculations with HTML and Javascript

I need help creating a simple HTML table for adding and subtracting values, with a limit of 10 and a minimum of 0. I have two functions set up, additionalAdd and additionalSub, triggered by the onclick event, but I keep getting an error saying that totalAd ...

How can the data from a factory be utilized within the same controller but in a separate file in an AngularJS application?

When trying to route from home.html to profile.html, there seems to be an issue with the execution of the profile.js script. I have data in script.js that is written in a factory. When the profile button is clicked, the page routes to profile.html, but the ...

The secondary angular button is failing to execute the function

Can someone help me with a small issue? I have two buttons on a webpage that should both contact a server. However, only the first button sends a HTTP request when clicked. <html> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8 ...