Welcome folks I am back with another blog post in which I will be implementing this Firebase Login in Android step by step in the form of Code. The Prerequisites of this tutorial is that you should have a working knowledge of working in Android Studio as we will be working in Android Studio to implement this project. So first thing Launch Android studio and make a new Project for ex FirebaseSignIn or anything you want.
So the No 1 step is to open Android Studio and create a brand new Project by clicking the start a new project option in the interface.
After opening Android studio and tapping on the Start a new Android Studio Project give a name for your Project and then click next two or three times to build your project
After you click next two or three times gradle the build system of Android will take some time to build your project so wait for some time as it prepares your project. So if all goes well you will get no errors and your build process will be over and you will get this screen.
So after building your project you have to connect your project to Firebase. In order to connect your project to Firebase two approaches are there first is manual and second one is automatic we will follow the automatic and much easier approach here as it’s also the recommended one. We will connect project to Firebase using the Firebase Assistant that is available in Android Studio. Go to Tools and select Firebase and follow the steps listed above
After selecting Firebase under the Tools you will be granted with the Firebase Assistant in which you have to select Email and Password Authentication in order to setup Authentication for your Android project.
After selecting email and password authentication you have to connect your app to Firebase by tapping the button of Connect to Firebase as shown in the figure below.
Just click on the button of Connect to Firebase and then the gradle will connect your project to Firebase in the background and also you will be granted with the screen of selecting the project to connect to Firebase or create a new one . Just click next and gradle will do the rest for you. If all goes well you will be granted with the following screen and green light will be there.
After this you have to setup Google Sign-In in order to do that you have to go to your Firebase project dashboard. Under Authentication you will find a option called as Sign-in method select Google Sign-In and simply enable it as shown below.
After that just go to this link and add these two dependencies into your build.gradle file which is there under gradle scripts just copy this line under dependencies section same to same
Note that the version has to be same for google play services and Firebase Auth this can be different for you depending on the version of Firebase Auth that is there on your computer for example if version is 16.0.0 then only copy paste this line in my case the version the version was 11.6.0 so I will copy paste
Add these lines of code into your activity_main.xml file in order to make a simple google Sign-In button into your App
After pasting this code into your project you will get a nice google sign-in button into your android app when you launch it see the screenshot below.
In this code we are just setting some things which include declaring Firebase Auth instance which will eventually kick start the Firebase Authentication for our project and secondly we are just binding a onClick listener to our signin button which is there in the interface so we will create this signin method later which will be invoked when we clicked this button.
Paste this code on the onCreate method of your activity. This code generally means we are setting google sign in options which includes requesting the id token and requesting some standard information from the user which includes email address,profile picture and username of the user.You can also read the documentation to customize this according to your application.
Paste this function code outside the onCreate method. This function is invoked whenever we press the sign-in button in our app. After pressing the button this function will invoke a window which will contain different accounts to select from. The first line is calling a getSignInIntent method on google sign in client object which will return a Intent which we are storing in intent object. Then we are passing this intent object to startActivityforResult method which is a very common method which takes the actual intent object and a constant which we have declared for this intent in the form of RC_SIGN_IN. I have given this constant a value of 1 you can give any value depending on your choice.
Paste this function code outside the onCreate method. This function onActivityResult is a standard method which returns the result of the intent that is passed to it when we invoke startforActivityResult method. It takes three arguments first the request code which we pass in the form of RC_SIGN_IN constant and the result code which tells the status of the intent whether it is passed or failed and thirdly the data associated with that particular intent. You can see in the above code we are comparing the request code to our request code if that’s the case then we will perform the operations on GoogleSignIn. We are invoking a method of getSignedInAccountFromIntent and we will pass the intent data to it. If the method runs successfully it will return a task object which then we can use to invoke getResult method which will return GoogleSignInAccount object which we will use as a parameter to the function firebaseAuthWithGoogle which will authenticate the personal data of our account to Firebase. This function we will create in the next step.
Paste this code outside the onCreate method this method firebaseAuthWithGoogle does all the hard work to connect our personal data to the firebase project and then synchronize it when this function executes successfully then we can safely say that we are successfully authenticated with Firebase. As you can see in the code also if all the things goes well then we are simply taking the data which is stored in the FirebaseUser object and then invoking some methods on that object to get username,email address and profile picture of the user and then calling the method updateUI which is responsibe to update the interface. We will create this method in the next step.
Paste this code outside the onCreate method. This function is responsible for updating the interface if we are successfully authenticated with firebase and also fetch the details about the user and display them. You can see in the following code we are not displaying the details of the user in this activity we are moving it to the profile Activity passing a intent object with some data of the user like username,email address and profile picture which we will display in the profile Activity.
Also paste these lines of code in the onStart method of the activity so that we can check if the user is already logged in or not. If the user is already logged in then we can automatically go to the profile Activity and display the details about the user but if the user is not logged in then if condition will evaluate to false then the user will be granted with the login screen.
In order to display the details about the user create a new Activity called ProfileActivity you can call it anything you want and copy paste the below xml code into your activity_profile.xml file.
This is the xml code of the Profile Activity class in this we have used one imageView to display the profile picture of the user and two text views in order to display the username and email address of the user and lastly a button is there to allow users to safely logout from their accounts.
In the Profile Activity copy paste the following code in this code we are just initializing the views that we have declared in xml code i.e. one imageview for profile picture and two textviews for email and username and one logout button we have binded a onclick listener to that button and called a simple logout method on FirebaseAuth object mAuth as simple as that the logout method is very simple as compared to the authentication part. Also we have used a method of intent.getStringExtra which fetches the data that is passed to the activity and after that we have just set the value of username and email address to the respective values.
Copy paste the following line into the build.gradle file this is the dependency of Picasso a famous library for displaying images from url , local file system into imageviews.
Copy paste this line into the onCreate method of the activity this line just fetches the image url and loads into the appropriate imageView.
Once again copy paste these lines of code into the onStart function but this time we will check if the user is already logged out then we will move the user to the login Activity as you can see in code also if that’s the case we are redirecting the user to MainActivity by the help of Intent but if the user is already logged in then display the details of the user.
Congratulations we are done making the Firebase Google Login in Android from scratch. All the source code of the Application is available below to download. Thanks for reading this post and if you like reading this and wants to read more of this please subscribe the blog below to get all the notications.
A programmer scientist by the mind and a passionate blogger by heart ❤️. Fountainhead of Coding Shiksha Life motto: Live while you can! Teach & inspire while you could & Smile while you have the teeth.Gautam Sharma