Android Firebase Authentication with Google
Welcome to a new post on Android Firebase Authentication!
This time we’ll take a look at how to authenticate your users using Google credentials.
Firebase Authentication is a powerful tool that lets users sign in to your app with ease using various providers like Google, Facebook, and Twitter. And in this post, we’re going to focus on Google Authentication, which is one of the most commonly used providers for Firebase Authentication in Android apps.
With Google Authentication, users can sign in to your app using their Google credentials, making the sign-in process fast and straightforward. In this post, I’ll show you how to set up Google Authentication in Firebase and implement it in your Android app. So, let’s dive in!
Short Recap of Firebase Authentication
Firebase Authentication is a cloud-based authentication service provided by Google that lets you authenticate users to your app using one or more of the various authentication providers it supports. Firebase Authentication handles the sign-in flow, provides UI elements for user authentication, and provides backend services to verify the user’s identity.
With Firebase Authentication, you can allow users to sign in with different providers like email/password, phone number, Google, Facebook, Twitter, and more. Firebase Authentication makes it easy to secure your app, protect user data, and personalise the user experience.
Overview of Google Authentication in Firebase
Google Authentication is one of the most commonly used authentication providers in Firebase, especially for Android apps. It allows users to sign in to your app using their Google account, so they don’t have to remember an additional username and password. Google Authentication is fast, easy to use, and provides a seamless sign-in experience for your users.
When you use Google Authentication in Firebase, you can also take advantage of other Google services like Google Analytics, Google Cloud Messaging, and Google Cloud Storage. Plus, you can easily manage your users’ authentication data using Firebase Authentication’s dashboard, making it easy to monitor and track user activity.
Setting up Google Authentication in Firebase
Now let’s start setting up our Firebase project, enabling the Google Authentication method and configuring our Android app with the new Firebase project.
A. Creating a Firebase Project
Before we can set up Google Authentication in Firebase, we first need to create a Firebase project. Here are the steps:
- Go to the Firebase Console (https://console.firebase.google.com/) and click “Add Project.”
- Enter your project name and select your country/region.
- Click “Create Project.”
Now that we have a Firebase project, let’s move on to enabling Google Authentication.
B. Enabling Google Authentication
To enable Google Authentication in Firebase, we need to configure the Firebase project to allow users to sign in with their Google credentials. Here’s how to do it:
- In the Firebase Console, select your project and click “Authentication” in the left-hand menu.
- Click the “Sign-in method” tab.
- Click on the “Google” provider and click the “Enable” toggle.
- Enter your app’s SHA-1 fingerprint and package name. To get the SHA-1 fingerprint, run the following command in your project directory:
keytool -exportcert -list -v \ -alias androiddebugkey -keystore ~/.android/debug.keystore
This will output your SHA-1 fingerprint. Be sure to replace ~/.android/debug.keystore
with the path to your debug keystore if you're using a custom one.
Another way you can get the SHA-1 fingerprint is by using the Gradle Console, as follows:
i. Open your Android Studio project and click on the “Gradle” tab on the right-hand side of the window.
ii. Expand the project name, then the “Tasks” folder, and then the “android” folder.
iii. Double-click on the “signingReport” task. This will run the task and generate the SHA-1 fingerprint in the Gradle Console.
To get your app’s package name, look for the package
attribute in your AndroidManifest.xml
file. Click “Save.”
Now that we have enabled Google Authentication in our Firebase project, let’s move on to configuring the project.
C. Configuring the Android app with the Firebase project
To use Google Authentication in your Android app, you’ll need to add the Firebase SDK to your project and configure it with your Firebase project settings. Here’s how to do it:
i. Add the Firebase SDK to your project by following the steps in the Firebase documentation for Android (https://firebase.google.com/docs/android/setup).
ii. In your project-level build.gradle
file, add the following line to the dependencies block:
This will add the Firebase Authentication library to your project.
iii. In your app-level build.gradle
file, add the following lines to the bottom of the file:
This applies the Google Services plugin to your project.
iv. Sync your project to download the dependencies and apply the plugin.
D. Adding the google-services.json
file to your app
The google-services.json
file contains your Firebase project's configuration details and is required to use Firebase services in your app. Here's how to add it to your app:
- Go to the Firebase Console, select your project, and click on the “Settings” icon next to “Project Overview” in the top-left corner. Then click “Project settings” and go to the “General” tab.
- Scroll down to the “Your apps” section and click the “Add app” button.
- Select “Android” as the platform and follow the prompts to register your app.
- Download the
google-services.json
file and move it to your project'sapp
directory.
Now that we have set up Google Authentication in Firebase and added the necessary dependencies and plugins to our project, we can move on to implementing Google Authentication in our app.
Implementing Google Authentication in the Android project
Now, it’s time to start implementing the Google authentication flow in our app by creating a Google sign-in button and wiring it to a sign-in function that calls the appropriate Firebase method.
A. Adding Google Sign-In Button to the UI
To add the Google Sign-In Button to your layout, you’ll need to update your XML layout file. Here is an example:
To set up the Google Sign-In Button in your activity, you’ll need to add the following code to your onCreate
method (I’ll explain the code in a second):
GoogleSignInOptions
is a configuration object that specifies the options for the Google sign-in process. Here, a newGoogleSignInOptions
object is created using the Builder pattern.- In the Builder object, the
requestIdToken
method is called with the value ofgetString(R.string.default_web_client_id)
as a parameter. This specifies that the sign-in process should request an ID token from the Google Sign-In API. R.string.default_web_client_id
is a string resource identifier that specifies the default web client ID for the Google Sign-In API. This ID is used to authenticate the app with Google and obtain an ID token for the signed-in user.- To provide the
default_web_client_id
value, you need to create a new project in the Google Developers Console and enable the Google Sign-In API for your project. Then, you need to create a web client ID by following the instructions in the Google Sign-In documentation. - Once you have created a web client ID, you need to add it to your Android project by adding a string resource to your project’s
strings.xml
file. - The
requestEmail
method is also called on the Builder object. This specifies that the sign-in process should request the user's email address. - A
GoogleSignInClient
is then created using theGoogleSignIn.getClient
method with the current activity (this
) and theGoogleSignInOptions
object created earlier as parameters. - In the listener’s
onClick
method, thesignInIntent
is obtained from theGoogleSignInClient
object using thegetSignInIntent
method. This intent is then used to start the sign-in flow by callingstartActivityForResult
method with a request codeRC_SIGN_IN
.
Overall, this code sets up the Google sign-in flow and starts the flow when the user clicks the sign-in button.
*Note: RC_SIGN_IN
is a constant used to identify the sign-in request. You can define this constant in your activity as follows: private static final int RC_SIGN_IN = 9001;
.
B. Authenticating with Google Credentials
Now we need to handle the sign-in result in onActivityResult
.
To do this, you’ll need to override the onActivityResult
method in your activity as follows:
firebaseAuthWithGoogle(account.idToken)
is the function that we will us to authenticate the user with Google, using Firebase. Here is the function implementation:
*Note: mAuth
is an instance of FirebaseAuth
that you'll need provide. You can do this in the onCreate method of your activity, or by using a custom Firebase manager.
Also, updateUI
is a method that you'll need to define to update the UI based on the user's sign-in status.
Conclusion
So, that was it!
Google Authentication is a fantastic way for Android developers to add secure user authentication functionality to their apps. Throughout this post, we’ve learned the basics of Google Authentication in Firebase, including how to set up a Firebase project and integrate Google Sign-In into your app.
Using Google Authentication in your Android app provides many benefits, including a smoother user experience, increased security, and easier user account management. Plus, since most users already have a Google account, the authentication process is more familiar and less likely to cause headaches during onboarding.
This being said, I hope you enjoyed this tutorial, and I also hope it helped you gain a better understanding of Android Firebase Authentication using Google.
Happy coding!