kreait/firebase-bundle
Symfony bundle integrating the Firebase Admin PHP SDK. Configure service accounts and access Firebase services (Auth, Firestore/Database, Messaging, Storage) via Symfony’s DI and configuration, with support for modern Symfony setups (Flex or manual).
Installation
composer require kreait/firebase-bundle:^6.1
Add the bundle to config/bundles.php:
Kreait\Firebase\Bundle\FirebaseBundle::class => ['all' => true],
Configuration
Define Firebase credentials in .env:
FIREBASE_CREDENTIALS=@file(/path/to/serviceAccountKey.json)
Or use a URL:
FIREBASE_CREDENTIALS=https://example.com/serviceAccountKey.json
First Use Case: AppCheck Integration
use Kreait\Firebase\Auth\Auth;
use Kreait\Firebase\Auth\AuthFactory;
$authFactory = new AuthFactory();
$auth = $authFactory->createAuth($credentials);
// Enable AppCheck with cached keyset
$auth->setAppCheckConfig([
'keyset_cache' => [
'handler' => new \Symfony\Component\Cache\Adapter\FilesystemAdapter(),
'ttl' => 3600, // Cache keyset for 1 hour
],
]);
Autowiring: The bundle auto-configures Auth, Database, Storage, and Messaging services.
public function __construct(
private Auth $auth,
private Database $database,
private Storage $storage
) {}
AppCheck Configuration Configure AppCheck with keyset caching in Symfony services:
# config/packages/kreait_firebase.yaml
kreait_firebase:
auth:
settings:
app_check:
keyset_cache:
handler: '@cache.app'
ttl: 3600
AppCheck Integration
$auth->verifyAppCheckToken($appCheckToken);
$auth = $factory->createAuth($credentials, [
'settings' => [
'app_check' => [
'keyset_cache' => [
'handler' => new \Symfony\Component\Cache\Adapter\RedisAdapter(),
'ttl' => 3600,
],
],
],
]);
User Authentication with AppCheck
$auth->verifyIdToken($idToken, [
'app_check_token' => $appCheckToken,
]);
AppCheck Keyset Cache
$factory->createAuth($credentials, [
'settings' => [
'app_check' => [
'keyset_cache' => [
'handler' => new \Symfony\Component\Cache\Adapter\ArrayAdapter(), // Fallback
'ttl' => 3600,
],
],
],
]);
Cache Provider Compatibility
CacheItemPoolInterface compliant adapters (e.g., FilesystemAdapter, RedisAdapter).AppCheck Token Validation
app_check_token in verification requests.$auth->verifyIdToken($idToken, ['app_check_token' => $appCheckToken]);
AppCheck Debugging Enable debug mode for AppCheck:
$auth->setAppCheckConfig([
'debug' => true,
'keyset_cache' => [...],
]);
Cache Validation Manually validate cache keyset:
$cache = new \Symfony\Component\Cache\Adapter\FilesystemAdapter();
$keyset = $cache->get('firebase_appcheck_keyset', function() {
return $auth->getAppCheckKeyset();
});
Custom Keyset Cache Implement a custom cache handler for AppCheck keyset:
$factory->createAuth($credentials, [
'settings' => [
'app_check' => [
'keyset_cache' => [
'handler' => new class implements \Psr\Cache\CacheItemPoolInterface {
// Custom cache logic
},
'ttl' => 3600,
],
],
],
]);
AppCheck Middleware Add middleware to validate AppCheck tokens globally:
$auth->setMiddleware(function(callable $next) {
return function($request) use ($next) {
if ($request->hasHeader('x-firebase-appcheck')) {
$appCheckToken = $request->getHeader('x-firebase-appcheck')[0];
$auth->verifyAppCheckToken($appCheckToken);
}
return $next($request);
};
});
Event-Driven AppCheck Listen to AppCheck-related events (e.g., keyset refresh):
$dispatcher->addListener(
FirebaseAppCheckKeysetRefreshedEvent::class,
fn(FirebaseAppCheckKeysetRefreshedEvent $event) => $this->invalidateCache()
);
How can I help you explore Laravel packages today?