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:

  1. Authenticating users using Firebase Auth
  2. 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.

#firebase #kotlin #mobile #authentication

Authenticate Users and Save Data in a Database using Firebase
4.40 GEEK