kreait / laravel-firebase

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

Invalid Service Account #147

Closed Zein0 closed 1 year ago

Zein0 commented 1 year ago

I'm trying to send a message yet I've had multiple errors regarding what I assume is Firebase credentials the recent error suggest

Invalid service account: Cannot use SplFileObject with directories", my .env

FIREBASE_CREDENTIALS='{"type": "service_account","project_id": "testgithub-gye656","private_key_id": "c054ef3104bb7cc9refwdqve","private_key": "-----BEGIN PRIVATE KEY-----****/N-----END PRIVATE KEY-----","client_email": "firebase-adminsdk-ergxs@testgithub-gye656.iam.gserviceaccount.com","client_id": "324212134324","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-ergxs%40testgithub-gye656.iam.gserviceaccount.com"}'
FIREBASE_DATABASE_URL="https://testgithub-gye656.firebaseio.com/"

FirebaseService

class FirebaseService
{
    public static function connect()
    {
        $firebase = (new Factory)
            ->withServiceAccount(base_path(env('FIREBASE_CREDENTIALS')))
            ->withDatabaseUri(env("FIREBASE_DATABASE_URL"));

        return $firebase->createMessaging();
    }
}
Techscq commented 1 year ago

The issue is with ur

**FIREBASE_CREDENTIALS**="JustTheFirebaseSDKpath.json"

FIREBASE_DATABASE_URL="https://testgithub-gye656.firebaseio.com/"

You're using the file content(JSON format) in a variable(it should work in other scenarios but not with .env variables), please use the path to the file. Store it on you're WORKSPACEPATH then set the variables like this =>

FIREBASE_CREDENTIALS=XNAME-firebase-adminsdk-.json

FIREBASE_DATABASE_URL=https://DBURL.firebaseio.com

Remember to move the Credentials file to a secure path on production ;), this should fix the issue if it doesn't try generating a new JSON Credential file, also don't use " on the firebase configuration vars

RizwanNaasir commented 1 year ago

I was using app('firebase.messaging'); but under the hood it tries to utilize env() directly. so just you should do this way

 public function connect(): Messaging
  {
      $firebase = (new Factory)->withServiceAccount(base_path(config('services.firebase.credentials')));
      return $firebase->createMessaging();
  }

as mentioned by @Techscq !