Authenticate Users and Save Data in a Database Using Firebase
Getting started with Firebase on Android
When you go about to build an application, there are a ton of considerations to take into account. And those are all mainly concerned with the client part. When you start to think about the server of your application, things can get pretty complicated. One option to alleviate some of that pressure is to use Firebase, in particular, two features from Firebase:
- Authenticating users using Firebase Auth
- Storing data using a Realtime Database
We will show in this article :
- How to build an Android application in Kotlin which authenticates users with Firebase Auth
- Use Retrofit2 to make requests to our server
- How to build a server in Node.js with Express that will receive requests from our application and fetch data from a Realtime Database in Firebase
If all of this might seem like a simple task, it isn’t. There is obviously a lot of setting up to do and handling various configurations, but here are also some pitfalls one would benefit from that will help save time and frustration.
Learn from my mistakes.
If you want to skip over all of the explanations, you can head over to the bottom of the article and see the entire source code there through the links.
The Setup
Our application will consist of both a front-end side and a back-end side. From the front-end perspective, there will be a login/signup page and another page that will fetch/send random data to our database. We will be using Firebase Authentication here to validate registered users. There are several ways to authenticate users:
- Email and password
- Google/Facebook/Twitter/GitHub account (what is called Federated Identity Provider Identification)
- Phone number
- Custom authorization
- Anonymous authorization
In our application, we will use the email and password option as it is the more straightforward approach and, in most cases, the more common solution.
This authentication will happen in our client and there will be no need for any communication to our back end for this task.
To make requests to the server, we will be using Retrofit2 by making GET requests. In these GET requests, we will be sending the data that needs to be updated alongside a token (more about the token in the Server section).
From the back-end side, our server is in charge of accepting requests from users using our application to either fetch/save/delete data (or CRUD). To be able to let authenticated users access the database, we will need to use Firebase’s Admin SDK. This framework will give us access to an API to verify authenticated users and pass requests to our database. We will be saving users data using Firebase’s Realtime Database. After all is done on the back-end side, we will be deploying it via Heroku.
The Client UI
After opening a new Kotlin project, we need to import some dependencies. First and foremost, you need to add Firebase to your project.
Follow the steps outlined here
Once that’s done, add the following dependency to your application level build.gradle
file:
When users open the application, they can either log in or sign up (if it is their first time). Since we have agreed that users will be validated based on a combination of their email and password, we’ll create a simple activity that has two EditTexts
for doing exactly that. We’ll also have two buttons to signify the choice to either signup or login.
- We are attaching listeners to our edit texts to identify when they have either lost focus or the user has pressed the done button.
- The
loginUser
method is in charge of, well, authenticating the user based on their prior credentials (using the signInWithEmailAndPassword API). - The
signupUser
method uses the createUserWithEmailAndPassword API. - You can see that we have overridden the
onStart
lifecycle method to identify when the user returns to the application and to update the UI appropriately if the user is already logged in.
When running our application, we will see this:

That was the easy part. Before we move on to writing the logic to communicate with the back end, let’s first build the back end.
The Server
We will be using Express when building our server. Below is a template for such a server, which also adds headers to bypass any CORS issues we might encounter :
Similar to what we did in the client, we also need to add Firebase to our Node server. If you recall the steps you took to create a project in Firebase and chose an Android project, you need to add to that project another application that will represent our server. By clicking on Add App on the main screen of Firebase console,

You will be presented with platforms to choose from:

You need to choose the web option (the one with the </> icon)
After doing the initial configuration inside the Firebase console, you will need to add the configuration object to your project:
We will place these configurations in our main file (app.js
).
There are some of you that might be thinking, “I am saving all this secret information in the client. It will be visible to everyone!” This is completely true, but in the case of Firebase, it is OK.
The Database
Yet alas, we still have some more configurations to do. This time it has to do with our Realtime Database. Head over to your Firebase console and choose the project you created earlier in this article. On the left menu, you will see a Realtime Database option. Click it.

What will follow next is that, on the right side, a window will load with the following tabs:

Under the Data tab, there will be your database’s URL. Remember it, as we will need to use it later on. The other important tab to look at is Rules. These rules specify who has access to your database and what they can do there. Initially (and for testing purposes), the rules there are pretty lax and let anyone read and write from your database. Before you make your application live, make sure to update these rules with something more restrictive. Don’t worry, you will get to see an example.
Firebase Admin
Next up, we need to set up Firebase Admin SDK. Since we already set up the necessary things in the Firebase console, we need to install the firebase admin package.
npm install firebase-admin --save
Next we need to generate a private key since our project is a service account. Inside Firebase Console:
- Next to Project Overview, there is a gear icon. Click it and choose Project Settings.

2. Click on the Service Accounts tab.
3. Click on the Create Service Account button.

4. Choose Node as the configuration snippet.
5. Click on Generate new private key.
Place this file inside your project and change the path to it in the code snippet provided by Firebase.
Note: Make sure to exclude this file in your .gitignore
file and to never upload it to any public repository.
Putting it all together, our app.js file
will look like this:
Remember the database URL I mentioned earlier? You will need to insert it inside the object you pass to Firebase’s admin initializeApp
method.
Create An Endpoint & Deployment
Phew, that was a lot of setting up. Right now, our server is able to run, but it won’t do anything since there is no endpoint configured. To fix that situation, let’s define one of our endpoints:
Our endpoint is called getData
and you can see that before doing any other logic, we are extracting the auth token sent and verifying it using Firebase Admin. If everything works correctly, we proceed with getting the user’s UID and using that to fetch the user’s data from the database.
Making Requests on the Client
As we mentioned earlier, we will be using Retrofit2 to make our requests to the server. I won’t go into detail about how to use Retrofit2 here (there are plenty of articles that do just that), so below you can find the standard implementation of making network requests using Retrofit2.
Notice that after getting the FirebaseUser object, we use the getIdToken
method to extract the token which will be sent to the server. In the same manner, we can create another GET request to set data in our database.
Last, but not least, here is an example of what a set of rules for your database might look like:
Here, we are making sure that any user that wants to read or write data is authorized.
This article is based on what I went through when building my own application. You can check it out below (the source code is also available):