kreait / laravel-firebase

A Laravel package for the Firebase PHP Admin SDK
https://github.com/kreait/firebase-php
MIT License
1.05k stars 169 forks source link

Firebase project [myproject_name] not configured #96

Closed almas-alright closed 3 years ago

almas-alright commented 3 years ago

its a bizarre exception Kreait\Laravel\Firebase\FirebaseProjectManager::configuration vendor/kreait/laravel-firebase/src/FirebaseProjectManager.php:41 throw new InvalidArgumentException("Firebase project [{$name}] not configured."); ` but when i changing default configuration at config > firebase.php

from :

'projects' => [ 'app' => [....] ]

to:

'projects' => [ 'myproject_name' => [....] ]

it works fine.

in my .env i have FIREBASE_CREDENTIALS=/path/abcd/firebase_cred.json FIREBASE_PROJECT=myproject_name now i want to know am i doing wrong anywhere in configuration? or something i missed to put in .env

jeromegamez commented 3 years ago

This does look correct from what I can see, so currently I'm as confused as you are 😅. Could you please share the contents of your config/firebase.php file, and also which versions of the kreait libraries you have installed? composer show | grep kreait

almas-alright commented 3 years ago

This does look correct from what I can see, so currently I'm as confused as you are . Could you please share the contents of your config/firebase.php file, and also which versions of the kreait libraries you have installed? composer show | grep kreait

@jeromegamez after composer show | grep kreait :

kreait/clock                         1.1.0              A PHP 7.0 compatible clock abstraction
kreait/firebase-php                  5.21.0             Firebase Admin SDK
kreait/firebase-tokens               1.16.0             A library to work with Firebase tokens
kreait/laravel-firebase              3.1.0              A Laravel package for the Firebase PHP Admin SDK

in config/firebase.php i have :

declare(strict_types=1);

return [
    /*
     * ------------------------------------------------------------------------
     * Default Firebase project
     * ------------------------------------------------------------------------
     */
    'default' => env('FIREBASE_PROJECT', 'airshipt-dcb94'),

    /*
     * ------------------------------------------------------------------------
     * Firebase project configurations
     * ------------------------------------------------------------------------
     */
    'projects' => [
        'myproject_name' => [
            /*
             * ------------------------------------------------------------------------
             * Credentials / Service Account
             * ------------------------------------------------------------------------
             *
             * In order to access a Firebase project and its related services using a
             * server SDK, requests must be authenticated. For server-to-server
             * communication this is done with a Service Account.
             *
             * If you don't already have generated a Service Account, you can do so by
             * following the instructions from the official documentation pages at
             *
             * https://firebase.google.com/docs/admin/setup#initialize_the_sdk
             *
             * Once you have downloaded the Service Account JSON file, you can use it
             * to configure the package.
             *
             * If you don't provide credentials, the Firebase Admin SDK will try to
             * auto-discover them
             *
             * - by checking the environment variable FIREBASE_CREDENTIALS
             * - by checking the environment variable GOOGLE_APPLICATION_CREDENTIALS
             * - by trying to find Google's well known file
             * - by checking if the application is running on GCE/GCP
             *
             * If no credentials file can be found, an exception will be thrown the
             * first time you try to access a component of the Firebase Admin SDK.
             *
             */
            'credentials' => [
                'file' => env('FIREBASE_CREDENTIALS', env('GOOGLE_APPLICATION_CREDENTIALS')),

                /*
                 * If you want to prevent the auto discovery of credentials, set the
                 * following parameter to false. If you disable it, you must
                 * provide a credentials file.
                 */
                'auto_discovery' => true,
            ],

            /*
             * ------------------------------------------------------------------------
             * Firebase Auth Component
             * ------------------------------------------------------------------------
             */

            'auth' => [
                'tenant_id' => env('FIREBASE_AUTH_TENANT_ID'),
            ],

            /*
             * ------------------------------------------------------------------------
             * Firebase Realtime Database
             * ------------------------------------------------------------------------
             */

            'database' => [
                /*
                 * In most of the cases the project ID defined in the credentials file
                 * determines the URL of your project's Realtime Database. If the
                 * connection to the Realtime Database fails, you can override
                 * its URL with the value you see at
                 *
                 * https://console.firebase.google.com/u/1/project/_/database
                 *
                 * Please make sure that you use a full URL like, for example,
                 * https://my-project-id.firebaseio.com
                 */
                'url' => env('FIREBASE_DATABASE_URL'),
            ],

            'dynamic_links' => [
                /*
                 * Dynamic links can be built with any URL prefix registered on
                 *
                 * https://console.firebase.google.com/u/1/project/_/durablelinks/links/
                 *
                 * You can define one of those domains as the default for new Dynamic
                 * Links created within your project.
                 *
                 * The value must be a valid domain, for example,
                 * https://example.page.link
                 */
                'default_domain' => env('FIREBASE_DYNAMIC_LINKS_DEFAULT_DOMAIN'),
            ],

            /*
             * ------------------------------------------------------------------------
             * Firebase Cloud Storage
             * ------------------------------------------------------------------------
             */

            'storage' => [
                /*
                 * Your project's default storage bucket usually uses the project ID
                 * as its name. If you have multiple storage buckets and want to
                 * use another one as the default for your application, you can
                 * override it here.
                 */

                'default_bucket' => env('FIREBASE_STORAGE_DEFAULT_BUCKET'),
            ],

            /*
             * ------------------------------------------------------------------------
             * Caching
             * ------------------------------------------------------------------------
             *
             * The Firebase Admin SDK can cache some data returned from the Firebase
             * API, for example Google's public keys used to verify ID tokens.
             *
             */

            'cache_store' => env('FIREBASE_CACHE_STORE', 'file'),

            /*
             * ------------------------------------------------------------------------
             * Logging
             * ------------------------------------------------------------------------
             *
             * Enable logging of HTTP interaction for insights and/or debugging.
             *
             * Log channels are defined in config/logging.php
             *
             * Successful HTTP messages are logged with the log level 'info'.
             * Failed HTTP messages are logged with the the log level 'notice'.
             *
             * Note: Using the same channel for simple and debug logs will result in
             * two entries per request and response.
             */

            'logging' => [
                'http_log_channel' => env('FIREBASE_HTTP_LOG_CHANNEL'),
                'http_debug_log_channel' => env('FIREBASE_HTTP_DEBUG_LOG_CHANNEL'),
            ],

            /*
             * ------------------------------------------------------------------------
             * HTTP Client Options
             * ------------------------------------------------------------------------
             *
             * Behavior of the HTTP Client performing the API requests
             */
            'http_client_options' => [
                /*
                 * Use a proxy that all API requests should be passed through.
                 * (default: none)
                 */
                'proxy' => env('FIREBASE_HTTP_CLIENT_PROXY'),

                /*
                 * Set the maximum amount of seconds (float) that can pass before
                 * a request is considered timed out
                 * (default: indefinitely)
                 */
                'timeout' => env('FIREBASE_HTTP_CLIENT_TIMEOUT'),
            ],

            /*
             * ------------------------------------------------------------------------
             * Debug (deprecated)
             * ------------------------------------------------------------------------
             *
             * Enable debugging of HTTP requests made directly from the SDK.
             */
            'debug' => env('FIREBASE_ENABLE_DEBUG', false),
        ],
    ],
];
jeromegamez commented 3 years ago

Alright, this actually makes sense - the value in default refers to a key in the projects array.

If we look at the default config:

return [
    'default' => env('FIREBASE_PROJECT', 'app'),
    'projects' => [
        'app' => [
            // ...
        ]
    ]
];

by default, the FIREBASE_PROJECT environment variable is not set, so it will default to app and find the configuration in the ['projects']['app'] field.

When you set the FIREBASE_PROJECT env variable to my_project_name, you will need a corresponding ['projects']['my_project_name'] field in the array:

# config/firebase.php
return [
    'default' => env('FIREBASE_PROJECT', 'app'),
    'projects' => [
        'app' => [
            // ...
        ],
        'my_project_name' => [
            // ...
        ]
    ]
];

and in your particular case, you would need:

# config/firebase.php
return [
    'default' => env('FIREBASE_PROJECT', 'airshipt-dcb94'),
    'projects' => [
        'airshipt-dcb94' => [
            // ...
        ]
    ]
];

If you're working with just one firebase project, I would recommend not setting the environment variable at all, and just setting the FIREBASE_CREDENTIALS=/path/abcd/firebase_cred.json environment variable. (In most cases, you shouldn't need to publish the config/firebase.php at all, by the way) After this, you can use the simple ways to access the Firebase services:

use Kreait\Laravel\Firebase\Facades\Firebase;

$auth = Firebase::auth();
$db = Firebase::database();
// ...

If you're using multiple projects, the FIREBASE_PROJECT env variable denotes the default project to use, but you'll still need to configure the different projects separately, for example like this:

# config/firebase.php
return [
    'default' => env('FIREBASE_PROJECT', 'main'),
    'projects' => [
        'main' => [
            'credentials' => [
                'file' => env('FIREBASE_MAIN_CREDENTIALS'),
            ]
        ],
        'second' => [
            'credentials' => [
                'file' => env('FIREBASE_SECOND_CREDENTIALS'),
            ]
        ],
    ]
];

and in your .env file:

# .env
FIREBASE_MAIN_CREDENTIALS=/path/to/main_credentials.json
FIREBASE_SECOND_CREDENTIALS=/path/to/second_credentials.json

In your could you could then access the different projects like this:

use Kreait\Laravel\Firebase\Facades\Firebase;

// The following two are equivalent
$mainAuth = Firebase::auth();
$mainAuth = Firebase::project('main')->auth();

$secondAuth = Firebase::project('second')->auth();

I hope this helps 🤞

nicolasvahidzein commented 2 years ago

This is very close to what i need, now how to access the FCM component and send the message...