laravel-firebase

repository·main·Indexed 23 days ago

https://github.com/beste/laravel-firebase

A Laravel package providing seamless integration for the Firebase PHP Admin SDK. It allows developers to interact with Firebase services—including Auth, Realtime Database, Firestore, Cloud Messaging, Remote Config, Cloud Storage, and App Check—using Laravel-native patterns such as Facades, Dependency Injection, and the FirebaseProjectManager for multi-project support.

Tokens
1.7K
Snippets
5
Records
9
Agent score
78%

What's inside laravel-firebase

  1. How to use Firebase components in Laravel

    main

    The package integrates with Laravel's Service Provider. Do not use the new Factory() pattern described in the standard Firebase PHP Admin SDK documentation. Instead, use Laravel's Dependency Injection, the Firebase Facade, or the app() helper to retrieve components.

    For detailed information on how to interact with specific Firebase services (like Auth, Firestore, etc.), refer to the Firebase PHP Admin SDK documentation.

  2. Publish Firebase configuration file

    main

    To customize the package configuration, publish the config/firebase.php file to your local config directory using the following Artisan command:

    php artisan vendor:publish --provider="Kreait\Laravel\Firebase\ServiceProvider" --tag=config
  3. Configure Firebase via environment variables

    main

    To authenticate with Firebase, you must provide a Service Account. The package uses environment variables starting with FIREBASE_ to configure services.

    At a minimum, you typically need to provide the Realtime Database URL:

    FIREBASE_DATABASE_URL=https://<your-project>.firebaseio.com

    You can also specify the path to your Service Account JSON file using FIREBASE_CREDENTIALS or GOOGLE_APPLICATION_CREDENTIALS to bypass auto-discovery.

    FIREBASE_DATABASE_URL=https://<your-project>.firebaseio.com
    FIREBASE_CREDENTIALS=storage/app/firebase-auth.json
  4. Configure credentials as an array

    main

    Instead of pointing to a JSON file, you can define your Service Account credentials directly as an array within config/firebase.php. This provides more granular control over the configuration items.

    'credentials' => [
        'type' => 'service_account',
        'project_id' => 'some-project-123',
        'private_key_id' => '123456789',
        'private_key' => '-----BEGIN PRIVATE KEY-----\nFOO_BAR_123456789\n-----END PRIVATE KEY-----\n',
        'client_email' => 'firebase-adminsdk-cwiuo@some-project-123.iam.gserviceaccount.com',
        'client_id' => '123456789',
        'auth_uri' => 'https://accounts.google.com/o/oauth2/auth',
        'token_uri' => 'https://oauth2.googleapis.com/token',
        'auth_provider_x509_cert_url' => 'https://www.googleapis.com/oauth2/v1/certs',
        'client_x509_cert_url' => 'https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-cwiuo%40some-project-123.iam.gserviceaccount.com',
        'universe_domain' => 'googleapis.com',
    ],
  5. Access multiple Firebase projects

    main

    You can configure multiple Firebase projects by adding additional sections to the projects array in config/firebase.php.

    By default, the Firebase facade accesses the default project. To access a specific project, use the project() method on the facade before calling the component method.

    use Kreait\\Laravel\Firebase\Facades\Firebase;
    
    // Return an instance of the Auth component for the default Firebase project
    $defaultAuth = Firebase::auth();
    
    // Return an instance of the Auth component for a specific Firebase project
    $appAuth = Firebase::project('app')->auth();
    $anotherAppAuth = Firebase::project('another-app')->auth();
  6. Configure Firebase project credentials

    main

    When configuring a project in firebase.projects.{name}, the credentials key can be provided in two ways:

    1. JSON File Path: A string representing a path to a service account JSON file. If the path is relative, it is resolved relative to the Laravel application base path.
    2. Credentials Array: An array containing the service account details (the raw JSON content structure).

    Example configuration structure:

    'firebase' => [
        'projects' => [
            'my-project' => [
                'credentials' => [
                    'file' => '/path/to/service-account.json',
                    // OR
                    // 'credentials' => ['type' => 'service_account', ...],
                ],
            ],
        ],
    ],
  7. Access specific Firebase projects via FirebaseProjectManager

    main

    The FirebaseProjectManager allows you to manage and retrieve multiple Firebase project instances. You can retrieve the default project or a specific project by its name as defined in your configuration.

    • project(?string $name = null): Returns the FirebaseProject instance for the given name. If no name is provided, it returns the default project.
    • getDefaultProject(): Returns the name of the default project configured in firebase.default.
    • setDefaultProject(string $name): Sets the default project name in the configuration.
  8. Access Firebase services via FirebaseProject

    main

    The FirebaseProject class serves as the primary entry point for interacting with various Firebase services within your Laravel application. It acts as a container for service instances, lazily initializing them via an underlying Factory when first requested.

    You can access the following services through the FirebaseProject instance:

    • auth(): Returns an Auth instance for authentication tasks.
    • database(): Returns a Database instance for Realtime Database operations.
    • firestore(): Returns a Firestore instance. If a specific Firestore database name was provided during project initialization, it will be used.
    • messaging(): Returns a Messaging instance for Cloud Messaging.
    • remoteConfig(): Returns a RemoteConfig instance for remote configuration.
    • storage(): Returns a Storage instance for Cloud Storage.
    • appCheck(): Returns an AppCheck instance.