I want to use Google’s Firestore to save user data, while the backend uses PHP. So far I configured the access rules like this:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null;
}
}
}
I followed google’s official documentation and my code looks like this:
$firestore = new FirestoreClient([
'projectId' => env('firebase.project.id'),
'keyfileName' => env('firebase.key.file')
]);
$collection = $firestore->collection('users');
$document = $collection->document($uid);
//this one triggers the permission error
$secret_key = $document->snaphot->get('secret_key');
Then I get:
PERMISSION_DENIED: Missing or insufficient permissions.
I tried to modify my rules like this:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth.uid != null;
}
}
}
No difference. By the way, the firebase authentication part works, I can create users and make them logged in. Furthermore, if I turn of the rules the error vanishes, but obviosly that is not a solution. I’m using CodeIgniter 4.5.5 with the most recent google/cloud-firestore
. When I will be back in my office, I can link all the dependencies. For now, I would appreciate any help.
2
Answers
Finally here is the solution.
I used
kreait/firebase-php
implementation of firestore. However make sure you fulfill all requirements listed in google's doc to make it work.Dependencies in
composer.json
:Firestore:
I don’t know if the typo is in your actual code, but I’m pretty sure that it should be
$secret_key = $document->snapshot->get('secret_key');
, not$document->snaphot
.