Powerful contact management for Android · Save smarter. Export faster.
Auto Contact Saver & Exporter
Tools Features How It Works Export Pricing Download
Get the App →

Category: How To

  • How to Integrate Firebase Authentication with Google Sign-In in an Android App

    Introduction

    Firebase Authentication is a robust solution for managing user sign-ins in mobile apps. By integrating it with Google Sign-In, you can provide a seamless and secure login experience for your users. Whether you’re building a chat app, e-commerce platform, or social network, this guide will help you implement Firebase Authentication with Google Sign-In in your Android project.


    Prerequisites

    Before diving into the integration, ensure you have the following:

    • Android Studio installed on your machine.
    • A Firebase account for managing your backend.
    • A basic understanding of Android app development.

    Setting Up Firebase

    Creating a Firebase Project

    1. Go to the Firebase Console.
    2. Click on Add Project and provide a project name.
    3. Follow the setup wizard to complete the project creation.

    Adding Your Android App to Firebase

    1. Navigate to Project Settings > General > Add App.
    2. Enter your app’s package name.
    3. Download the google-services.json file and add it to your app’s app/ directory.

    Adding Firebase to Your Android Project

    To integrate Firebase, include the following in your build.gradle files:

    1. Project-Level build.gradle:

    gradledependencies {
    classpath 'com.google.gms:google-services:4.3.15'
    }
    [adinserter name=”Block 1″]

    1. App-Level build.gradle:

    // Import the Firebase BoM
    implementation platform(‘com.google.firebase:firebase-bom:33.7.0’)

    // TODO: Add the dependencies for Firebase products you want to use
    // When using the BoM, don’t specify versions in Firebase dependencies
    implementation ‘com.google.firebase:firebase-analytics’

    implementation ‘com.google.android.gms:play-services-auth:21.3.0’

    implementation(“com.google.firebase:firebase-auth”)

     

    Configuring Google Sign-In in Firebase

    1. Open the Firebase Console.
    2. Navigate to Authentication > Sign-in Methods.
    3. Enable Google Sign-In and save changes.
    4. Copy the Web client ID under your app’s OAuth 2.0 Client IDs.

    Designing the UI

    Create a simple and attractive UI with a Google Sign-In button. Here’s an example layout in XML:

     

    <LinearLayout
    android:id="@+id/googleSignInBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:background="#E9E9E9">

     

    <ImageView
    android:id=”@+id/imgGLogo”
    android:layout_width=”40dp”
    android:layout_height=”40dp”
    android:src=”@drawable/google_logo”/>


    <TextView
    android:id="@+id/txtSignIn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="6dp"
    android:text="Sign In With Google"
    android:textColor="#0D2A75"
    android:textStyle="bold"/>
    </LinearLayout>

    [adinserter name=”Block 2″]

    Implementing Google Sign-In Logic

    Configuring GoogleSignInOptions

    In your MainActivity:

     GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
    .requestIdToken(getString(R.string.default_web_client_id))
    .requestEmail()
    .build();
    mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

    Authenticating Users with Firebase

    Handling Sign-In

    Trigger the Google Sign-In intent:

     

    private void signIn(){

    Intent signInIntent = mGoogleSignInClient.getSignInIntent(); 

    startActivityForResult(signInIntent, RC_SIGN_IN);

     }

    Authenticating with Firebase

    Upon successful sign-in:

     

    private void handleSignInResult(Task<GoogleSignInAccount> task) {
    try {
    GoogleSignInAccount account = task.getResult(ApiException.class);
    AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
    mAuth.signInWithCredential(credential)
    .addOnCompleteListener(this, task -> {
    if (task.isSuccessful()) {
    FirebaseUser user = mAuth.getCurrentUser();
    Toast.makeText(this, “Welcome, ” + user.getDisplayName(), Toast.LENGTH_SHORT).show();
    }
    });
    } catch (ApiException e) {
    Log.w(“SignInError”, e.getMessage());
    }
    }[adinserter name=”Block 3″]

    Managing User Sessions

    Check User Login Status

    FirebaseUser currentUser = mAuth.getCurrentUser();
    if (currentUser != null) {
    // User is logged in
    }

    Sign Out

    mGoogleSignInClient.signOut();
    mAuth.signOut();

    Conclusion

    Integrating Firebase Authentication with Google Sign-In adds a professional touch to your app, ensuring secure and convenient access for users. Follow this guide to streamline the process and offer a great user experience.


    FAQs

    1. How secure is Firebase Authentication?
    Firebase uses industry-standard security measures, ensuring safe user authentication.

    2. Can I use other sign-in methods with Firebase?
    Yes, Firebase supports various methods like email, phone, and social logins.

    3. What happens if a user revokes access?
    Firebase automatically prevents further access to the app for that user.

    4. Is Google Sign-In free to use?
    Yes, Google Sign-In and Firebase Authentication are free, but usage limits apply.

    5. How can I troubleshoot authentication failures?
    Check logs, verify your configuration, and ensure valid SHA-1 keys.