Javatpoint Logo
Javatpoint Logo

Android Firebase Authentication - Google Login

Firebase Authentication provides backend services for easy use the SDKs and in-built UI libraries to authenticate the user in the application. Most of the apps need the identity of the user, and after knowing their status, the app saves the user's data securely in the cloud. It aims to build a secure authentication system.

Using Firebase Authentication, we will authenticate the log-in of Google, Facebook, GitHub, Twitter and more.

In this tutorial, we will integrate the Firebase Authentication for Google Sign-In functionality in our Android application using Google and Firebase APIs.

Steps to create and configure Android App on Google Firebase Account

1. Create a Firebase developer account at https://firebase.google.com/ and click on 'GO TO CONSOLE'.

2. Click on 'Add project'.

3. Fill the project name and select the analytics location, cloud Firestore location, accept the controller terms and click 'Create project'.


Android Firebase Authentication - Google Login

4. When your new project is successfully ready click on 'Continue'.


Android Firebase Authentication - Google Login

5. Select the 'Android' platform SDK.


Android Firebase Authentication - Google Login

6. Register your app to Firebase by providing required app information and click on 'Register app'.

We can get the app certificate SHA-1 key through following steps:

  1. Open Android project.
  2. Open the Gradle tab from a right side panel.
  3. Double click on 'signingReport'.
  4. We will found our app SHA-1 key on 'Gradle Console'.

Android Firebase Authentication - Google Login

7. Now download the 'google-services.json' file to integrate it into the Android application and click on 'Next'.


Android Firebase Authentication - Google Login

8. Add the firebase SDK dependencies in .gradle files of your application and click 'Sync now' in the IDE and click on 'Next'.


Android Firebase Authentication - Google Login

build.gradle(Project)

build.gradle (Module)

AndroidManifest.xml

Add the Internet permission in the AndroidManifest.xml file to access the network connection.

9. Run your app to verify the installation configuration, if everything is fine it displays a success message and then click on 'Continue to console'.


Android Firebase Authentication - Google Login

10. At console page select Authentication -gt; Sign-in method -gt;Google -gt; Enable and click on 'Save'.


Android Firebase Authentication - Google Login

Android Firebase Authentication with Google Sign-In Example

In this example, we will integrate the Firebase Authentication with Google Sign-In in our Android app. Once the user successfully login through Google Sign-In, we will authenticate them with Firebase Authentication and then redirect the user to the next activity (ProfileActivity) and display their detail.

We need to paste the downloaded 'google-services.json' file in our Android project app directory.

activity_main.xml

Add the following code in an activity_main.xml file. In this activity, we implement the custom Google Sign-in button.

strings.xml

Add your web client id in the string.xml file. Even you can use the default Google web client id in the program.

MainActivity.java

Add the following code in MainActivity.java class. The functionality of classes and methods used in MainActivity.java class are given below:

  1. The new FirebaseAuth.AuthStateListener(){} is used as an authentication listener to check whether the user is sign-in or not. If the users successfully sign-in we place other firebase logic.
  2. The new GoogleSignInOptions.Builder() method configure the Google Sign-in to get the user data using the requestEmail option.
  3. Clicking on Sign-In button, it calls Auth.GoogleSignInApi.getSignInIntent(googleApiClient) and starting the intent with startActivityForResult().
  4. The onActivityResult() we get the result of Google Sign-in request.
  5. If handleSignInResult(result) returns true, we get the Google Authentication using idToken.
  6. Calling firebaseAuthWithGoogle(credential) for making Firebase Authentication with Google.

If the Firebase authentication is success, we redirect the user to the next activity (ProfileActivity.java).

profile_activity.xml

Add the following code in the profile_activity.xml file. The ImageView is used for display the user profile image, TextView is for name, email, id and a button for log-out.

ProfileActivity.java

In this ProfileActivity.java class, we get the user information and display them. The GoogleSignInResult class implements the Result interface which represents the final result of invoking an API method of Google Play Services.

The GoogleSignInAccount class keeps the necessary information of the user.

Output:

Android Firebase Authentication - Google Login Android Firebase Authentication - Google Login Android Firebase Authentication - Google Login Android Firebase Authentication - Google Login
Next Topic#




Please Share

facebook twitter google plus pinterest

B.Tech / MCA


Learn Latest Tutorials