Firebase is a mobile and web application development platform. It provides services that a web application or mobile application might require. Firebase provides email and password authentication without any overhead of building the backend for user authentication. Google Sign-In is a secure way to authenticate users in your apps. It reduces the hassle of dealing with and handling those extra passwords by the user to get authenticated to the app. Firebase offers a great number of options to implement Login in your app like Email, Phone number, Google, Facebook, etc.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Connect your app to Firebase
After creating a new project in Android Studio. Connect your app to Firebase. For connecting your app to firebase. Navigate to Tools on the top bar. After that click on Firebase. A new window will open on the right side. Inside that window click on Authentication and then email and password authentication.
After clicking on email and password authentication you will get to see the below screen. Inside this screen click on the first option connect to firebase and after that click on the second option to add Firebase authentication to your app.
Add the following dependencies if it is not already present there.
Step 3: Enable Google Sign In
Step 4: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
Step 5: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Step 6: Create a new empty activity
Please refer to Create New Activity in Android Studio and name the activity as DashboardActivity.
Step 7: Working with the activity_dashboard.xml file
Go to the activity_dashboard.xml file and refer to the following code. Below is the code for the activity_dashboard.xml file. Create a Button which when clicked, logs out the user from the app.
Step 8: Working with the DashboardActivity.kt file
Go to the DashboardActivity.kt file and refer to the following code. Below is the code for the DashboardActivity.kt file. Comments are added inside the code to understand the code in more detail.