I’ve recently started using Firebase more and more. One of the first challenges I had was the ability to tell if a user just signed up or not. It seems like an easy problem, and I have solved it time and time again on the “back end”. However with Firebase you techincally are not managing a back end, so you have to do everything in the front end logic.
At first I was just simply saying to update the data of a user everytime they visited the profile page. However this could potentially use more data and bandwidth on my Firebase than I wanted to. Since I am currently using the free version of Firebase, this becomes more of an issue.
I decided that we I would just do a simple database check first instead of pushing all the data each time.
First we set our firebase reference.
Alright here we get the authorization data from Firebase for the current user, and then run it through the getUser function to store the userid.
This is the function that we are going to call with the boolean value of whether or not the user exists. Then in here we can do what we want. For example on my site I store data to the database if they are new, but not if they are a returning user.
Here is the meat. This function simply calls the Firebase reference and sees if the user exists. Then we pass that data to the callback function.
Simple yet efficient, and hopefully will save me just a little bit of bandwidth.
To see the full code check out: https://gist.github.com/shadowcodex/dcfe7d11b2e8cb0ca51d
Shannon Duncan is the Founder and Author at Unrestricted Coding. He mentors and teaches people of all ages how to code and enjoy the art of programming. Find him on twitter, linked-in, and github.
Have you used Firebase? How was the experience? Share your feedback with us in the comment section below!