How to Use FireBase in your Android Project

There are two ways using which you can add FireBase to your project-

1) Manually

2) Using SDK

We will discuss using the SDK only.

First include google-services plugin in your root level build.gradle file

Then, in your module Gradle file (usually the app/build.gradle), add the apply plugin

Here we add the FireBase dependency:
Now, you are ready to use the FireBase in your project.

Google plus login using Firebase

To do the Google+ login first add:

to your app level gradle file, now your app gradle file will look like:

STEP 2: Enable Google plus login corresponding to your project

Open Firebase console -> Authentocation Section -> enable Google login -> click save

When you are done with the above steps, now it’s time to write some code.

To do the Google plus login you need the Authentication with Firebase. For that we need to configure Google Sign-in

In your MainActivity add the following code in OnCreate() method:

In the above code you need to create the GoogleSignInOptions instance, where you can pass the token, request the user’s email, profile etc.
As soon you enable the Google plus login in Firebase console, the corresponding token has been auto created by Google in Google Developer console. which you can pass it inside requestIdToken. Best part is you don’t even need to include the token inside your project just write the exact line i.e
getString(R.string.default_web_client_id) inside requestIdToken method.

Now you need to create the instance of GoogleApiClient like below:

Here what you are doing is you are telling Google that we are going to use the GoogleSignInAPI so we need to add this API using GoogleApiClient.

As we have our GoogleApiClient ready we can pass it in the intent and pass that intent in startActivityForResult().

The call back method will look like:

In the above call back method we are getting the result from the GoogleSignInAPI in the GoogleSignInResult. If we are getting the success inside the result then get the account from that result and pass the account in firebaseAuthWithGoogle() as parameter for authentication.

Inside the firebaseAuthWithGoogle() method we fetch the AuthenticationCredential like below:

using the passed account you can get the user’s emailId, name etc. like

so the firebaseAuthWithGoogle() method will look like:

we need to add the call back method also inside it:

In this we need to pass the authCredential. The result of the authentication will come in this call back method only. Where we can check the success and failure using the conditions and show appropriate messages:

This is how we are able to login via Google plus using FireBase.