Firebase Emulator UI

repository·master·Indexed 43 days ago

https://github.com/firebase/firebase-tools-ui

A web-based management interface for the Firebase Emulator Suite. It allows developers to visually inspect and edit data in Cloud Firestore and Realtime Database, view system logs, and manage Authentication users. Requires Firebase CLI version >= 8.4.0.

Tokens
11.5K
Snippets
41
Records
55
Agent score
55%

What's inside Firebase Emulator UI

  1. Connect the Emulator UI development server to a local project

    master

    If you want to iterate on the UI while connected to your own project's emulators, follow these steps:

    1. In your project directory, start the emulators:
    cd project/
    firebase emulators:start --import my-data
    1. In a separate terminal, navigate to the firebase-tools-ui directory and start the dev server, providing your project ID and the emulator hub address:
    cd firebase-tools-ui/
    GCLOUD_PROJECT=<project-id> FIREBASE_EMULATOR_HUB=localhost:4400 npm start

    Note: The development server runs on port 5173 by default. Do not confuse this with the production Emulator UI, which defaults to port 4000.

    GCLOUD_PROJECT=<project-id> FIREBASE_EMULATOR_HUB=localhost:4400 npm start
  2. Start the Emulator UI development server with test data

    master

    To develop the Emulator UI using pre-defined test data, use the firebase emulators:exec command. This ensures the necessary environment variables are set so the web application can communicate with the emulators.

    1. Clone the repository and install dependencies:
    git clone https://github.com/firebase/firebase-tools-ui.git
    cd firebase-tools-ui
    npm install
    1. Run the development server:
    firebase emulators:exec --project demo-example --import test-data 'npm start'
    1. Access the UI at http://localhost:5173.
    firebase emulators:exec --project demo-example --import test-data 'npm start'
  3. Use the Firebase Emulator UI

    master

    The Firebase Emulator UI is a web interface for the Firebase Emulator Suite. It provides an overview of running emulators, a Data Viewer/Editor for Firebase Realtime Database and Cloud Firestore, and a Logs Viewer with filters.

    To use it, follow the standard guide for installing and running the Emulator Suite.

    Requirements:

    • Firebase CLI version >= 8.4.0.

    The Emulator UI starts automatically when you start the Emulator Suite.

  4. Understand Realtime Database routing structure

    master

    The Realtime Database UI uses a specific URL pattern to navigate namespaces and data paths. This allows for deep-linking into specific nodes of the database.

    URL Patterns:

    • Base Redirect: Navigating to the base database path (e.g., /${url}/${projectId}) automatically redirects to the /data endpoint.
    • Namespace and Data Path: The primary routing pattern is /${path}/:namespace/data/:path*.

    Example Path Construction: If the base URL is /database and the project ID is my-project, the path to a specific node users/123 in a namespace default would be: /database/default/data/users/123

  5. Understand ExpressionPathSegmentValue segments

    master

    An ExpressionPathSegmentValue represents a single segment within a path expression. Segments can be one of three types:

    1. Simple Path: A direct path segment (e.g., /simple).
    2. Capture: A single-segment capture expression (e.g., /{name}). It includes a variableName and an optional boundValue (a URL-decoded string).
    3. Glob Capture: A multi-segment capture expression using glob syntax (e.g., /{name=**}). It includes a variableName and a boundValue which is itself a list of ExpressionPathSegmentValue segments.
  6. Structure of Firestore Security Rules evaluation results

    master

    When debugging Firestore Security Rules via the Emulator UI, the evaluation results are represented by the FirestoreRulesEvaluation object. This object provides the context of the request, the final outcome, and granular details about which rules allowed or denied the request.

    Key components include:

    • rulesContext: The state of the world during evaluation (request, resource, path, method, and time).
    • outcome: The final result of the evaluation ('allow', 'deny', 'error', or 'admin').
    • granularAllowOutcomes: An array of OutcomeInfo objects indicating specific lines where rules were evaluated.
    // Example of the structure of a FirestoreRulesEvaluation
    {
      requestId: "unique-request-id",
      outcome: "allow",
      rulesContext: {
        request: { /* ... ExpressionValue ... */ },
        resource: { /* ... ExpressionValue ... */ },
        path: "/users/123",
        method: "get",
        time: "2023-01-01T00:00:00Z"
      },
      granularAllowOutcomes: [
        { line: 10, outcome: "allow" }
      ],
      rules: "service cloud.firestore { ... }",
      time: "2023-01-01T00:00:00Z"
    }
  7. Navigate between different Firestore databases via URL

    master

    The Firestore Emulator UI supports multiple databases by modifying the databaseId in the URL path. The routing structure follows this pattern:

    • Data View: /firestore/:databaseId/data (e.g., /firestore/default/data)
    • Requests View: /firestore/:databaseId/requests (e.g., /firestore/default/requests)

    If you navigate to /firestore without a database ID, the UI redirects to /firestore/default/data by default.

  8. Configure Authentication Emulator Settings

    master

    The EmulatorV1ProjectsConfig interface defines the configuration for the Authentication emulator, mirroring the Firebase CLI configuration. Currently, it supports configuring email sign-in behavior.

    interface EmulatorV1ProjectsConfig {
      signIn: {
        allowDuplicateEmails: boolean;
      };
    }
    const config: EmulatorV1ProjectsConfig = {
      signIn: {
        allowDuplicateEmails: false
      }
    };
  9. Build the Emulator UI for production

    master

    The npm run build command generates production assets for both the server and the web client.

    • Web Client: Output is located in dist/client. It is minified, optimized, and includes content hashes in filenames.
    • Server: Output is located in dist/server/server.js. This is a standalone Node.js file containing all dependencies.

    To run the production build with emulators:

    firebase emulators:exec --project demo-example --import test-data 'node dist/server/server.js'

    This starts a server that serves both the static files and APIs at http://localhost:5173/.

    Warning: The static files in dist/client are not intended for direct deployment to a CDN or website; they must be served via the production server described above.

    firebase emulators:exec --project demo-example --import test-data 'node dist/server/server.js'
  10. Run tests with the Emulator Suite

    master

    To run tests in an environment with emulators, use firebase emulators:exec.

    Run all tests:

    firebase emulators:exec --project demo-test 'npm test'

    Run a specific test file (e.g., for Firestore):

    firebase emulators:exec --project demo-test --only firestore 'npm test AddCollectionDialog.test.tsx'

    Run tests without interactive watch mode:

    firebase emulators:exec --project demo-test --only firestore 'npm test -- --watchAll=false'

    Note: If you encounter port conflict errors, ensure other instances of the Firebase Emulator Suite are stopped.

    firebase emulators:exec --project demo-test 'npm test'