A React Native application that generates AI images with Firebase authentication, Backblaze B2 storage, and a credit-based generation system.
- User Authentication (Firebase)
- Image Storage (Backblaze B2)
- Metadata Storage (Firestore)
- Credit-based Image Generation
- Daily Credit Refreshes
- User-specific Image Gallery
- Real-time Updates
- Each user receives 25 credits daily
- One credit generates one image
- Credits refresh automatically every 24 hours
- Unused credits are replaced during refresh
- Total generated images are tracked
- Transaction-safe credit operations
- Secure credit management through Firestore Rules
- Node.js and npm installed
- React Native development environment set up
- Firebase project created at Firebase Console
- Backblaze B2 account and bucket created at Backblaze
- Create a new Firebase project at Firebase Console
- Enable Email/Password Authentication:
- Go to Authentication > Sign-in method
- Enable Email/Password provider
- Create Firestore Database:
- Go to Firestore Database
- Create database
- Start in production mode
- Choose a location closest to your users
- Set up Firestore Rules:
- Copy the contents of
firestore.rulesto your Firebase Console - Deploy the rules
- Copy the contents of
- Create a Backblaze B2 account if you haven't already
- Create a new bucket:
- Set bucket name
- Choose "Private" for file visibility
- Create an application key:
- Go to App Keys
- Create a new application key
- Set key capabilities to: "Read and Write"
- Restrict to bucket you created
- Note down:
- Endpoint
- Bucket name
- Application Key ID
- Application Key
-
Copy
.env.templateto.env:cp .env.template .env
-
Fill in the required values in
.env:Firebase Configuration:
FIREBASE_API_KEY=your_firebase_api_key FIREBASE_AUTH_DOMAIN=your_project_id.firebaseapp.com FIREBASE_PROJECT_ID=your_project_id FIREBASE_STORAGE_BUCKET=your_project_id.firebasestorage.app FIREBASE_MESSAGING_SENDER_ID=your_sender_id FIREBASE_APP_ID=your_app_id FIREBASE_MEASUREMENT_ID=your_measurement_idBackblaze B2 Configuration:
BACKBLAZE_ENDPOINT=your_b2_endpoint BACKBLAZE_BUCKET_NAME=your_bucket_name BACKBLAZE_KEY_ID=your_application_key_id BACKBLAZE_APP_KEY=your_application_keyFAL AI Configuration:
FAL_API_KEY=your_fal_api_key FAL_API_URL=your_fal_api_url
-
Install dependencies:
npm install
-
Start the development server:
npm start
The application implements comprehensive security rules to protect user data and ensure proper credit management:
match /credits/{userId} {
// Only owner can read their credits
allow read: if isOwner(userId);
// Initial credit creation with 25 credits
allow create: if isOwner(userId)
&& isValidCredit()
&& request.resource.data.balance == 25;
// Allow updates for daily refresh or credit usage
allow update: if isOwner(userId)
&& isValidCredit()
&& (
// Daily refresh to 25 credits
(request.resource.data.balance == 25
&& !isWithinLastDay(resource.data.lastRefill))
||
// Deduct one credit for generation
(request.resource.data.balance == resource.data.balance - 1)
);
}match /images/{imageId} {
// Read access for active images
allow read: if isAuthenticated()
&& resource.data.userId == request.auth.uid
&& resource.data.status != 'deleted';
// Create new image if user has credits
allow create: if isAuthenticated()
&& request.resource.data.userId == request.auth.uid
&& exists(/databases/$(database)/documents/credits/$(request.auth.uid))
&& get(/databases/$(database)/documents/credits/$(request.auth.uid)).data.balance > 0;
}match /requests/{requestId} {
// Track API calls and ensure credit availability
allow create: if isAuthenticated()
&& request.resource.data.userId == request.auth.uid
&& exists(/databases/$(database)/documents/credits/$(request.auth.uid))
&& get(/databases/$(database)/documents/credits/$(request.auth.uid)).data.balance > 0;
}The app uses modern SDKs and best practices:
-
Authentication:
- Firebase Authentication for user management
- Protected routes and secure sessions
-
Storage:
- Backblaze B2 for image storage
- Signed URLs for secure image access
- Automatic cleanup on image deletion
-
Database:
- Firestore for image metadata and credits
- Transaction-safe credit operations
- Real-time updates and queries
- User-specific data isolation
-
Credit System:
- Daily credit refreshes
- Transaction-protected credit usage
- Secure credit management
- User-friendly credit display
-
App Structure:
/components- React components/config- Firebase and Backblaze configuration/utils- Utility functions/hooks- Custom React hooks
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Create a new Pull Request
All sensitive configuration is stored in environment variables:
- Never commit the
.envfile to version control - Use
.env.templateas a reference - Keep your credentials secure
- Regularly rotate API keys for enhanced security