Skip to main content

Authenticate with Firebase Using Google Sign-In

To enable Google Sign-In authentication in your News App, follow these steps:

  1. Open your Firebase console.

  2. Go to Authentication and select the Sign-in method tab.

  3. Find the Google provider and enable it. This will automatically set up Google sign-in.

    Enable Google Auth

    Google Auth Settings

  4. Download and install JDK x64 Installer from Oracle's website and set JAVA_HOME by following these instructions if you haven't done so yet.

  5. In Android Studio, right-click on the android folder, then right-click on gradlew and select "Open In > Terminal" as shown below:

    Open Terminal

  6. In the terminal, enter the command to generate a debug SHA1 key. Copy this key and add it to your Firebase console.

    SHA1 Command

    Add SHA1 to Firebase

  7. Press the save button to enable Google authentication in your app.

With these settings in place, users will be able to sign into your News App using their Google accounts.

Configure Android

  1. Update your android/app/build.gradle file to include Google Sign-In dependencies:
dependencies {
implementation 'com.google.android.gms:play-services-auth:20.3.0'
// Other dependencies
}
  1. Make sure your SHA-1 fingerprint is registered in the Firebase project settings

Configure iOS

  1. Open your iOS project in Xcode
  2. Update the Info.plist file to include necessary URL schemes for Google Sign-In
  3. Configure the GoogleService-Info.plist file

Add Package Dependencies

  1. Add the required packages to your pubspec.yaml file:
dependencies:
firebase_auth: ^latest_version
google_sign_in: ^latest_version
  1. Run flutter pub get to install these dependencies

Implementation Example

Here's a simple example of how to implement Google Sign-In:

import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';

Future<UserCredential> signInWithGoogle() async {
// Trigger the authentication flow
final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();

// Obtain the auth details from the request
final GoogleSignInAuthentication? googleAuth = await googleUser?.authentication;

// Create a new credential
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth?.accessToken,
idToken: googleAuth?.idToken,
);

// Once signed in, return the UserCredential
return await FirebaseAuth.instance.signInWithCredential(credential);
}
tip

Test Google Sign-In thoroughly on both Android and iOS platforms to ensure a smooth user experience.