Skip to content

Firebase Setup

Firebase is a platform developed by Google for creating mobile and web applications. It provides a variety of services like authentication, real-time database, cloud storage, and more. In this section, we will set up Firebase for our Flutter project.

Pre-requisites

  1. Install Node.js from here
  2. Install Firebase CLI by running the following command in your terminal:
bash
npm install -g firebase-tools
  1. Login to Firebase by running the following command in your terminal:
bash
firebase login
  1. Install FlutterFire cli by running the following command in your terminal:
bash
dart pub global activate flutterfire_cli

Firebase Setup

  1. In the root of your project, run the following command:
bash
flutterfire configure
  1. Select the Firebase project you want to use for this Flutter project, or create a new one.

  2. Enable android and ios platforms for Firebase Authentication in the cli.

  3. Go to firebase console, select your project, and navigate to Authentication tab. Then click on Get Started to enable Firebase Authentication. Then click on Sign-in method tab and enable email, google, and apple sign-in methods.

Firebase Auth Signing Method Setup

  1. Generate SHA-1 and SHA-256 keys for your app by running the following command in your terminal:

In MacOS:

bash
keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android

In Windows:

bash
keytool -list -v -keystore "\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android

In Linux:

bash
keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
  1. Copy the SHA-1 and SHA-256 keys and paste them in the Firebase console under Project settings -> General -> Your apps -> SHA certificate fingerprints.

Firebase Auth SHA Keys Setup

  1. Enable Firestore Database, Analytics, Storage, Crashlytics, and Firebase Cloud Messaging in the Firebase console.

  2. Download the google-services.json file and replace the existing file created by flutterfire_cli in the android/app directory of your Flutter project.

  3. Copy the client id for web and android from your google-services.json or firebase_options.dart file and paste it into the .env file under GOOGLE_CLIENT_ID_APP and GOOGLE_CLIENT_ID_WEB variables.

  4. Go to Firebase dashboard and enable Firestore database, Storage, Analytics, Crashlytics one by one. You may need to download the google-services.json file again after this.

Firebase Functions Setup

The boilerplate includes Firebase Cloud Functions that automatically manage user data when users sign up or delete their accounts. These functions create and delete user documents in the Firestore users collection.

What the Functions Do

The Firebase Functions in the functions directory provide:

  1. onUserCreated: Automatically creates a user document in Firestore when a new user signs up via Firebase Authentication
  2. onUserDeleted: Automatically deletes the user document from Firestore when a user account is deleted

Prerequisites

Ensure you have Firebase CLI installed and are logged in (covered in the pre-requisites section above).

Deploy Firebase Functions

  1. Navigate to the functions directory in your project:
bash
cd functions
  1. Install the required dependencies:
bash
npm install
  1. Build the TypeScript functions:
bash
npm run build
  1. Deploy the functions to Firebase:
bash
firebase deploy --only functions

Verify Deployment

  1. Go to the Firebase Console
  2. Select your project
  3. Navigate to Functions in the left sidebar
  4. You should see two deployed functions:
    • onUserCreated
    • onUserDeleted

Function Details

The functions automatically handle:

  • User Creation: When a user signs up, creates a document in the users collection with:

    • User ID, email, display name, photo URL, phone number
    • Authentication provider information
    • Created and updated timestamps
  • User Deletion: When a user account is deleted, removes the corresponding document from the users collection

These functions ensure your Firestore database stays in sync with Firebase Authentication automatically.