Skip to content

Poorgramer-Zack/dart-expert-skills / skills/firebase

integrating-firebase

Verified

Firebase integration for Flutter including Auth, Firestore, FCM, Cloud Storage, Analytics, and Crashlytics. Use when implementing serverless backend features, real-time sync, or push notifications.

FileKindSize
SKILL.mdskill md3.0 KB
references/analytics-crashlytics.mdmarkdown3.0 KB
references/app-check.mdmarkdown2.9 KB
references/auth.mdmarkdown6.2 KB
references/cost.mdmarkdown4.2 KB
references/database.mdmarkdown3.5 KB
references/messaging.mdmarkdown4.2 KB
references/serverpod-bff.mdmarkdown4.3 KB
references/setup.mdmarkdown2.7 KB
references/storage.mdmarkdown2.8 KB

references/serverpod-bff.md

View on GitHub
---
metadata:
  last_modified: "2026-03-12 11:18:17 (GMT+8)"
---

# 08 - Serverpod (BFF Architecture)

## Goal
Completely bypass the limitations of Firestore Security Rules and safely integrate third-party APIs (Stripe, OpenAI) by routing the Flutter app's Firebase interactions through a highly secure Backend-For-Frontend (BFF) built in Dart using Serverpod.

## The Architecture
Instead of using the standard Firebase Client SDKs to interact with Firestore, the Flutter app only communicates with Serverpod generated Endpoints. 

Serverpod runs on a cloud server (like Google Cloud Run), initializes the `firebase_admin_sdk` package, and wields absolute administrative authority to read/write to Firestore.

### Why do this?
1. **Ditch Security Rules:** Authoring complex authorization logic in `.rules` files is extremely error-prone. With this architecture, you write all authentication checks in standard, debuggable Dart code on the Serverpod backend.
2. **Hide API Keys:** If you need to charge a user via Stripe when they create a document, putting the Stripe Secret Key in the Flutter app is an instant security breach. In this architecture, the Secret Key lives safely on the Serverpod backend.
3. **End-to-End Type Safety:** Data traversing from Client to Server is cryptographically armored by Serverpod YAML protocol files.

## Instructions

### 1. Serverpod Backend Initialization (`myapp_server`)
Add the `firebase_admin_sdk` package to your Serverpod backend dependencies. Initialize it during server startup.

```dart
// myapp_server/lib/server.dart
import 'package:firebase_admin_sdk/firebase_admin_sdk.dart';

void run(List<String> args) async {
  // 1. Initialize the Admin App using a Service Account Key
  // (Store the JSON contents in Serverpod's Passwords.yaml or ENV vars)
  final admin = FirebaseApp.initializeApp(
    options: AppOptions(
      projectId: 'your-project-id',
      credential: Credential.fromServiceAccountParams(
        clientId: '...',
        email: '...',
        privateKey: '...',
        projectId: 'your-project-id',
      ),
    ),
  );
  
  // 2. Instantiate Admin services
  final firestore = admin.firestore();
  
  // 3. Inject them into Serverpod's context globally or let endpoints construct them
  final pod = Serverpod(args, ...);
  
  // Example: Store it globally so endpoints can grab it
  // pod.customData['firestore'] = firestore; 
  
  await pod.start();
}
```

### 2. Creating the Secure Endpoint
In the backend, create an endpoint that handles complex business logic (like processing a payment AND updating Firestore simultaneously).

```dart
// myapp_server/lib/src/endpoints/user_transaction_endpoint.dart
class UserTransactionEndpoint extends Endpoint {
  
  // This endpoint is called from the Flutter Client
  Future<TransactionResult> processPurchase(Session session, String userId, double amount) async {
    // 1. You are running on a secure server. Nobody can decompile this code.
    
    // 2. Interact with Third-Party Payment API
    final paymentSuccess = await StripeService.chargeCard(userId, amount);
    
    if (!paymentSuccess) {
      return TransactionResult(success: false); // Defined in a .yaml protocol
    }

    // 3. Write securely to Firestore using ADMIN privileges. No .rules apply here!
    // Initialize Firestore admin (or grab it from session.server.customData)
    final firestore = admin.firestore(); // `admin` is the FirebaseApp created at startup; share it via a top-level variable or session.server.customData
    
    await firestore.collection('receipts').add({
      'userId': userId,
      'amount': amount,
      'timestamp': DateTime.now().toIso8601String(),
    });
    
    // 4. Return strongly-typed success to Flutter
    var result = TransactionResult(success: true, receiptId: "...");
    return result;
  }
}
```

## Constraints
*   **Dual Dependency Awareness:** The Flutter Client will STILL use the `firebase_auth` Client SDK to authenticate the user and obtain a JWT token. The Client then passes this JWT Token to the Serverpod Endpoint, which uses `firebase_admin_sdk` to verify that the token is legitimate before executing backend code.
*   **Admin Power:** The Admin SDK ignores ALL Firestore Security Rules. Ensure your Dart logic inside the Serverpod Endpoint rigorously checks if the user making the request actually has the authority to modify the targeted documents.
deprecation-and-migrationaddyosmani/agent-skillsManages deprecation and migration. Use when removing old systems, APIs, or features. Use when migrating users from one implementation to another. Use when migrating a database schema in production, such as renaming or dropping a column without downtime (expand/contract). Use when deciding whether to maintain or sunset existing code.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
performance-optimizationaddyosmani/agent-skillsOptimizes application performance across frontend, backend, queries, and databases. Use when performance requirements exist, when you suspect performance regressions, when Core Web Vitals or load times need improvement, when N+1 query patterns need fixing, or when profiling reveals bottlenecks.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
agent-cost-reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json.Stars: 118kUpdated Claude CodeCodexVerifiedInstall
agent-cost-reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json.Stars: 118kUpdated Claude CodeCodexVerifiedInstall