简体   繁体   中英

How to integrate Firebase Auth with PHP app?

I have a simple PHP API which is used to authenticate users that use my app. I have been trying to use Firebase Auth, without any success. Following the documentation found here , I came to a point where:

  • I have created the Firebase project in the Firebase console.

  • I have created a Service Account, for which I have downloaded the Secret .json file that contains the credentials needed.

  • I have placed the file in the root directory of my app.

And I use this code to interact with the database:

    $serviceAccount = ServiceAccount::fromJsonFile(__DIR__.'/path/to/file.json');

    $firebase = (new Factory)
        ->withServiceAccount($serviceAccount)
        ->createAuth();

When dumping the $firebase instance I get this:

^ Kreait\Firebase\Auth {#78 ▼
  -client: Kreait\Firebase\Auth\ApiClient {#55 ▶}
  -tokenGenerator: Firebase\Auth\Token\Generator {#58 ▶}
  -idTokenVerifier: Kreait\Firebase\Auth\IdTokenVerifier {#77 ▶}
}

Which makes me believe the connection is successful, but when I try to list all the users

    $firebase->listUsers();

I get the following

^ Generator {#42 ▼
   this: Kreait\Firebase\Auth {#78 …}
   executing: {▼
     C:\Users\user\Projects\Simple-Api\vendor\kreait\firebase- 
php\src\Firebase\Auth.php:110 {▼
       Kreait\Firebase\Auth->listUsers(int $maxResults = 1000, int $batchSize = 1000): Generator …
       › {
       ›     $pageToken = null;
       ›     $count = 0;
     }
   }
   closed: false
 }

And when I try to loop through all the users like this:

   foreach ($users as $user) {
         var_dump($user);
     }

I get these error:

cURL error 60: SSL certificate prblm: unable to get local issuer certificate
cURL error 77: SSL certificate prblm: unable to get local issuer certificate

TEMPORARY FIX (not suggested for production):

  • Go to Guzzle Client.php class
  • Set verify to false

If you continue to follow the documentation, you will find

https://firebase-php.readthedocs.io/en/stable/user-management.html#list-users

Auth::listUsers() returns a Generator, which you can either loop through with foreach() or convert to an array with iterator_to_array() - you should use the latter only if you limit the resultset, otherwise you will run into performance issues when PHP tries to load thousands of user records into memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM