<!-- Generated from the rendered page by scripts/write-llm-mirrors.mjs. Do not edit by hand. -->
Canonical: https://molo17.com/blog/combined-use-of-firebase-authentication-firebase-cloud-firestore-in-android-part-1/
Markdown mirror: https://molo17.com/blog/combined-use-of-firebase-authentication-firebase-cloud-firestore-in-android-part-1/index.md
Title: Firebase Authentication and Cloud Firestore in Android - MOLO17
Description: First part of the series on how to combine Firebase Authentication and Cloud Firestore in an Android project, in which you will see ho to use Firebase Auth.

[← All articles](/blog/)

Article

# Combined use of Firebase Authentication and Firebase Cloud Firestore in Android – Part 1

29 Mar 2019  [Knowledge sharing](/blog/?category=knowledge-sharing)  4 min read

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 \[…\]

![](https://molo17.com/wp-content/uploads/2023/05/AdobeStock_232932620-1-1.png)

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](\"https://firebase.google.com/\") 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](\"https://firebase.google.com/\") 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:

![\\"Add](\"https://lh4.googleusercontent.com/joIcyfgCLILG61cvCsAxPxwLpuwBAFvJwCeZyMZI7LM_7t-tm2KFapyGUU60RXmkzR0RyxPW4lI_Ql9OhSv2PQk6N8uhpOFyRoizChlaP1shQ5F9amzp8aBnXjkO0lPCam9Abr8\")

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:

![\\"Choose](\"https://lh6.googleusercontent.com/4rOBCECVcS5zLRmNp3Rj0GcvcGDGvvgsvN3rwZRj4zSU6Fkpzq8cjQxTLXh-jMNI3kjdH8T-qbLG-sQNKAlEZR0XxSHFNg6Ppy2WhA7cHFF4XN8vchYzXwW5nQwdxItpRKn-zPU\")

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**:

![\\"Dependency](\"https://lh4.googleusercontent.com/xuC8XZs-zp1VeJNSA9OGyk5-qJQQZHr7v2dkxC9l__bL6ObFrFarZLH8JFyD0acEZkNhTTTdeF2xCDk8GlDbwF4n9harNw7O7_03alcXzHo0WpBl_Nr1Q86qS_crwZEqqRcJ0lM\")

Finally add the library dependencies to the app module **build.gradle**:

![\\"Library](\"https://lh6.googleusercontent.com/ZDMp_NWAW6LGrkC45l0DjU6n-DzxK2dRK4n5KiYjSPCMCV3SL6tSeQlhmygm5v9kv-9cW0mt44y7zrsijZqYra5w7I9OV_rCs_ZnE237kfGIebPou32KWsl2fURHM_9rIYixV4E\")

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:

![\\"Login](\"https://molo17.com/wp-content/uploads/2019/03/Screenshot_1552151244-576x1024.png\")

In this specific case we have three simple text field and two button, moreover in my example this activity implements the`` `OnCompleteListener<AuthResult>` `` from `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](\"https://molo17.com/2019/04/combined-use-of-firebase-authentication-firebase-cloud-firestore-on-android-part-2/\")!

You can find all the code in this Github repository: [https://github.com/MOLO17/AuthFirestorePOC](\"https://github.com/MOLO17/AuthFirestorePOC\")

[← Older article Kotlin inline classes, such magic](/blog/kotlin-inline-classes-such-magic/) [Newer article → A trip into Kotlin Multiplatform Projects – Part 3: an every day app](/blog/kotlin-a-trip-into-multiplatform-projects-part-3/)

## Keep reading

1.  [Knowledge sharing Migrating from Couchbase Server, community edition (CE) to Neptune Introduction Neptune for Couchbase is our Database-as-a-Service sporting the best performance, availability and scalability ever. It pairs well with our support plans for further assistance from our qualified technicians. \[…\] 21 Jul 2022](/blog/migrating-from-couchbase-server-community-to-neptune/)
2.  [Knowledge sharing Accelerating Couchbase NoSQL database adoption in SMBs with MOLO17 MOLO17, company focused on data mobilization thanks to its capabilities to unleash data potential connecting, through open standards and proprietary solutions, mobile users need with legacy and or closed \[…\] 6 Aug 2021](/blog/accelerating-couchbase-nosql-database-adoption-in-smbs-with-molo17/)
3.  [Knowledge sharing #4 Discover Couchbase: your first app with Couchbase Lite In the previous articles we had a first look at Couchbase Lite and we discovered how to integrate it into a mobile app. Moreover, we got aware of Couchbase \[…\] 7 Feb 2020](/blog/your-first-app-with-couchbase-lite/)

[Back to all articles](/blog/) [More in Knowledge sharing →](/blog/?category=knowledge-sharing)
