Firebase

Firebase

Flood provides seamless integration with Firebase (opens in a new tab), allowing you to easily incorporate Firebase services into your Flutter application. This documentation will guide you through the process of setting up Firebase in your Flood project and utilizing its various features.

Initialization

To get started with Firebase in your Flood project, follow these steps:

  1. Create a Firebase project in the Firebase Console (opens in a new tab).

  2. Use the flutterfire_cli (opens in a new tab) CLI tool to configure the connection to your Firebase project. Run the following command in your terminal:

    flutterfire configure

    This command will guide you through the process of connecting your Flutter app to your Firebase project.

  3. Create your Firestore database in the Firebase Console. Navigate to the "Firestore Database" section and follow the prompts to create a new database.

  4. Enable Email/Password authentication in the Firebase Console. Go to the "Authentication" section, select the "Sign-in method" tab, and enable the "Email/Password" provider.

Usage

To initialize the connection to Firebase in your Flood project, add the FirebaseCoreComponent as one of the initialCoreComponents in the FloodCoreComponent:

example_core/lib/pond.dart
FloodCoreComponent(
  initialCoreComponents: [
    FirebaseCoreComponent(),
    // ... other initial components
  ],
  // ... other configuration
)

This will ensure that the connection to Firebase is established before the rest of the CorePondComponents are registered.

Authentication

Flood's Auth Module integrates with Firebase Authentication (opens in a new tab) to provide secure user authentication in your app.

Admin Accounts

In Firebase, accounts are considered "admins" if they have a custom claim titled "admin". To set this custom claim on specific accounts, you need to create a Firebase Function and execute it manually.

Here is an example of a Firebase Function that would add the "admin" custom claim to a user whenever you add a document to the "admin" collection with a "uid" property:

example_core/firebase/functions/src/index.ts
import * as admin from 'firebase-admin';
import * as functions from 'firebase-functions';
 
admin.initializeApp();
 
export const addAdminClaim = functions.firestore
    .document('admin/{adminId}')
    .onCreate(async (snapshot, context) => {
        const adminData = snapshot.data();
        const uid = adminData.uid;
 
        try {
            await admin.auth().setCustomUserClaims(uid, { admin: true });
            console.log(`Admin claim added to user with UID: ${uid}`);
 
            // Update the document with "complete: true"
            await snapshot.ref.update({ complete: true });
            console.log(
                `Document updated with "complete: true" for UID: ${uid}`
            );
        } catch (error) {
            console.error(
                `Error adding admin claim to user with UID: ${uid}`,
                error
            );
        }
    });

Usage

To use Firebase Authentication in your Flood project, add the FirebaseAuthServiceImplementation as one of the authServiceImplementations in the FloodCoreComponent:

example_core/lib/pond.dart
FloodCoreComponent(
  authServiceImplementations: (corePondContext) => [
    FirebaseAuthServiceImplementation(),
  ],
)

This will configure Firebase Authentication as the implementation for the AuthService.static.cloud() method, enabling Firebase Authentication in cloud environments.

Firestore

Flood seamlessly integrates with Firestore (opens in a new tab), allowing you to store and retrieve data from your Firestore database. It also provides powerful automation capabilities to streamline your development workflow.

Usage

To use Firestore as the implementation for your repositories in cloud environments, add the FirebaseCloudRepositoryImplementation as one of the repositoryImplementations in the FloodCoreComponent:

example_core/lib/pond.dart
FloodCoreComponent(
  repositoryImplementations: (corePondContext) => [
    FirebaseCloudRepositoryImplementation(),
  ],
)

This will configure Firestore as the implementation for the Repository.cloud() method, enabling Firestore as the data storage solution in cloud environments.

Automatic Firestore Security Rules Generation and Deployment

Flood's Automate Module provides a convenient way to automatically generate Firestore security rules based on your Drop repositories and deploy them directly to your Firebase project using the Ops Module.

To generate and deploy Firestore security rules, run the following command:

dart tool/automate.dart deploy production

This command will analyze your Drop repository security rules, generate the corresponding Firestore security rules, and deploy them to your Firebase project associated with the "production" environment.

The generated security rules will be based on the access control and validation logic defined in your Drop repositories, ensuring that your Firestore data is properly secured and follows the specified rules.

Firebase Storage

Flood's Asset Module seamlessly integrates with Firebase Storage when using the cloud or adapting asset providers in cloud environments. This integration allows you to easily store and manage assets like images, videos, and other files in your Firebase project.

Usage

To use Firestore as the implementation for your repositories in cloud environments, add the FirebaseCloudRepositoryImplementation as one of the repositoryImplementations in the FloodCoreComponent:

example_core/lib/pond.dart
FloodCoreComponent(
  assetProviderImplementations: (corePondContext) => [FirebaseStorageAssetProviderImplementation()],
)

This will configure Firebase Storage as the implementation for the AssetProvider.cloud() method, enabling Firebase Storage as the data storage solution in cloud environments.

Automatic Firebase Storage Security Rules Generation and Deployment

Flood's Automate Module provides a convenient way to automatically generate Firebase Storage security rules based on your AssetProviders and deploy them directly to your Firebase project using the Ops Module.

To generate and deploy Firebase Storage security rules, run the following command:

dart tool/automate.dart deploy production

This command will analyze your AssetProvider security rules, generate the corresponding Firebase Storage security rules, and deploy them to your Firebase project associated with the "production" environment.

The generated security rules will be based on the access control and validation logic defined in your AssetProviders, ensuring that your Firebase Storage data is properly secured and follows the specified rules.

Firebase Crashlytics

Flood integrates with Firebase Crashlytics (opens in a new tab) to capture and report crashes and errors in your app.

Usage

When you use the FloodAppComponent in your Flood project, Firebase Crashlytics integration is automatically added. The Flood toolkit pulls logs from the Log Module and sets the Firebase Crashlytics user identifier to the currently logged-in user from the Auth Module.

To ensure that Firebase Crashlytics is properly set up, make sure you have added the necessary dependencies and configuration files to your project as per the Firebase Crashlytics documentation.