Nowadays, nearly all Android projects we work on use REST Service. Using methods like GET, POST or DELETE you can send, receive or delete data from a Backend. But what you think if i told you that there’s a solution if you cannot have your own Backend or if there is some path to the request that confuses you? Don’t get me wrong, REST is great, but Google comes to simplify our lives with the Firebase suite with few components like Firebase Authentication and Firebase Cloud Firestore!
Firebase works similar to hosting, you choose what services you need, connect them in your application and Whoa! You’re ready to see people who use your app all around the world! This is AWESOME! ?
Your goal for this tutorial is to build an app that handle users sign-in/login and their basic info (like employee’s role). In this first part you’ll focus on Firebase Authentication.
Getting Started
In order to start working, you need to Add Firebase dependencies to your Android project.
To be able to add them you need also a file called google-services.json, that contains all the configuration Firebase uses internally. Then, let’s start creating a new Firebase Projects!
First of all, go to Firebase website and login using a Google account and when ready click the Go to console in the upper right corner of the page. You’ll now create a new Firebase Project clicking on Add project button like below:
Choose your project name and select your current country.
Finally click on Create Project button. After the project creation confirm message click Continue. Now you should see the project dashboard! Next you have to add an Android application to the project, then click on the Android badge to add it:
Compile the package name field with your app package (in my case is: com.fm.federico.authfirestorepoc
) and click on Register App. Finally follow the instructions on the Firebase page to add the google-services.json file to the project and click Next.
Now you can enable the Authentication service of Firebase. Click on Authentication in the left bar and select Access Method tab. You have multiple choice of authentication, we will use the simple e-mail and password method then click on E-Mail/Password, enable and save your choice. Under Users tab you will now be able to add new users with their e-mail and password, every time you will add a user firebase will assign an UID. The UID will be used in Firestore to identify which role the single user belongs to (you will use it in the second part of the tutorial).
It’s time to add Gradle Dependencies, let’s go! ?
First of all add the following dependency to the project-level build.gradle:
Finally add the library dependencies to the app module build.gradle:
Hit Sync now and let Gradle do The Magic! ?
Now, Hands on code!
Here we go, the final step of this first part of the tutorial. In order to handle Register and Login you have to create the UI of the app that let the user insert e-mail and password for Login and, in case, the role for Register like:

In this specific case we have three simple text field and two button, moreover in my example this activity implements the
from OnCompleteListener<AuthResult>
com.google.android.gms.tasks.OnCompleteListener
to handle the completion for the login or sign-in task of our app.
For the Login button you will use the signInWithEmailAndPassword method of Firebase that start the authentication with the service. You can set the click listener for the button and call the method with:
login_button.setOnClickListener { auth.signInWithEmailAndPassword( email_edit_text.text.toString(), password_edit_text.text.toString() ).addOnCompleteListener(this) }
While, for the Register button, you will use the createUserWithEmailAndPassword method of Firebase that start the creation of a new account with the service. You can set the click listener for the button and call the method with:
register_button.setOnClickListener { auth.createUserWithEmailAndPassword( email_edit_text.text.toString(), password_edit_text.text.toString() ).addOnCompleteListener(this) }
In both case auth is an instance of class FirebaseAuth that you can retrieve with FirebaseAuth.getInstance()
Finally, in your onCompletion implementation you can start the flow you desire when the task of authentication or registration is successful or failed. In my example my implementation is like:
override fun onComplete(task: Task<AuthResult>) { if (task.isSuccessful) { MainActivity.newInstance(this).let(::startActivity) finish() } else { Toast.makeText( baseContext, "Authentication failed.", Toast.LENGTH_SHORT ).show() } }
Finally… Firebase Authentication setted up
Firebase automatically login the user if choose to start the registration flow, then you don’t have to start the login flow after the registration. Just relax Firebase is here for you! ?
Here we go, the app for the first part of this tutorial is ready! You can run and enjoy it!
Thanks for reading and stay tuned for the second part where we see the integration with Cloud Firestore!
You can find all the code in this Github repository: https://github.com/MOLO17/AuthFirestorePOC