Firebase Setup Guide¶
ShopSync uses Firebase for authentication, real-time data synchronization, and cloud storage. This guide will walk you through setting up Firebase for your ShopSync development environment.
Overview¶
Firebase services used by ShopSync:
- Firebase Authentication - Google Sign-In, email/password auth
- Cloud Firestore - Real-time database for lists and items
- Firebase Hosting - Web app deployment (optional)
- Cloud Functions - Server-side logic (future feature)
Prerequisites¶
- Google account
- ShopSync source code cloned locally
- Basic understanding of Firebase console
Step 1: Create Firebase Project¶
- Go to Firebase Console
Visit console.firebase.google.com
-
Create New Project
-
Click Add project
- Enter project name:
ShopSync(or your preferred name) -
Click Continue
-
Configure Google Analytics (Optional)
-
Toggle Google Analytics on/off based on your preference
- If enabled, select or create an Analytics account
-
Click Create project
-
Wait for Setup
Firebase will take a few moments to create your project.
Step 2: Enable Authentication¶
-
Navigate to Authentication
In the Firebase console sidebar, click Authentication.
-
Get Started
Click Get started button.
-
Enable Sign-in Methods
Enable the following providers:
- Click Google in the providers list
- Toggle Enable
- Enter project support email
- Click Save
- Click Email/Password
- Toggle Enable
- Click Save
-
Configure OAuth Consent Screen (if prompted)
- Set app name: ShopSync
- Add support email
- Save and continue
Step 3: Create Firestore Database¶
- Navigate to Firestore Database
Click Firestore Database in the sidebar.
- Create Database
Click Create database.
- Set Security Rules
Choose Start in test mode for development:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.time < timestamp.date(2024, 12, 31);
}
}
}
!!! warning "Production Security" Test mode rules allow anyone to read/write your database. Before deploying to production, update with proper security rules (see below).
- Select Location
Choose a Cloud Firestore location close to your users:
nam5(United States)eur3(Europe)asia-southeast1(Singapore)
!!! important "Cannot Change Location" This location cannot be changed later, so choose carefully.
- Enable Database
Click Enable to create the database.
Step 4: Register Your App¶
Android App¶
-
Add Android App
-
In Project Overview, click the Android icon
-
Register app with package name:
com.aadishsamir.shopsync(or your custom package name fromandroid/app/build.gradle) -
Download google-services.json
-
Download the
google-services.jsonfile -
Move it to:
android/app/google-services.json -
Add Firebase SDK
The ShopSync project already has Firebase dependencies configured. Verify in android/build.gradle:
And in android/app/build.gradle:
- Add SHA-1 Certificate
For Google Sign-In to work, add your SHA-1 certificate:
# Debug certificate
keytool -list -v -alias androiddebugkey \
-keystore ~/.android/debug.keystore
# Default password: android
Copy the SHA-1 fingerprint and add it in Firebase Console:
- Project Settings → Your apps → Android app
- Click Add fingerprint
- Paste SHA-1
- Click Save
iOS App (Optional)¶
-
Add iOS App
-
Click the iOS icon in Project Overview
-
Register app with bundle ID:
com.aadishsamir.shopsync -
Download GoogleService-Info.plist
-
Download the configuration file
-
Add to:
ios/Runner/GoogleService-Info.plist -
Update Xcode Project
Open ios/Runner.xcworkspace in Xcode and verify the plist is included.
Web App¶
-
Add Web App
-
Click the Web icon
< / >in Project Overview - Register app with nickname:
ShopSync Web -
Check Also set up Firebase Hosting (optional)
-
Copy Configuration
Firebase will show a JavaScript configuration object. You'll need this for web deployment.
- Update Firebase Options
The configuration is already set up in lib/firebase_options.dart. If you need to regenerate it:
Step 5: Configure FlutterFire CLI (Recommended)¶
The FlutterFire CLI automates Firebase setup:
- Install FlutterFire CLI
- Run Configuration
-
Select Options
-
Choose your Firebase project
- Select platforms (Android, iOS, Web)
- CLI will update
firebase_options.dart
Step 6: Set Up Security Rules¶
For production, use proper Firestore security rules:
Firestore Rules¶
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// Helper function to check authentication
function isSignedIn() {
return request.auth != null;
}
// Helper function to check if user owns the document
function isOwner(userId) {
return isSignedIn() && request.auth.uid == userId;
}
// Users collection
match /users/{userId} {
allow read: if isSignedIn();
allow write: if isOwner(userId);
}
// Shopping lists
match /shoppingLists/{listId} {
allow read: if isSignedIn() &&
(resource.data.ownerId == request.auth.uid ||
request.auth.uid in resource.data.sharedWith);
allow create: if isSignedIn() &&
request.resource.data.ownerId == request.auth.uid;
allow update, delete: if isSignedIn() &&
resource.data.ownerId == request.auth.uid;
// Items subcollection
match /items/{itemId} {
allow read, write: if isSignedIn() &&
(get(/databases/$(database)/documents/shoppingLists/$(listId)).data.ownerId == request.auth.uid ||
request.auth.uid in get(/databases/$(database)/documents/shoppingLists/$(listId)).data.sharedWith);
}
}
// List groups
match /listGroups/{groupId} {
allow read, write: if isSignedIn() &&
resource.data.userId == request.auth.uid;
}
// Categories
match /categories/{categoryId} {
allow read: if isSignedIn();
allow write: if isSignedIn() &&
resource.data.userId == request.auth.uid;
}
// Recycle bin
match /recycleBin/{userId}/items/{itemId} {
allow read, write: if isOwner(userId);
}
}
}
To update rules:
- Go to Firestore Database → Rules
- Paste the rules above
- Click Publish
Storage Rules (if using Cloud Storage)¶
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /user-uploads/{userId}/{allPaths=**} {
allow read: if request.auth != null;
allow write: if request.auth != null && request.auth.uid == userId;
}
}
}
Step 7: Test the Setup¶
- Run the App
-
Test Authentication
-
Launch the app
- Click Sign in with Google
-
Verify successful sign-in
-
Test Firestore
-
Create a shopping list
- Add items
-
Check Firebase Console → Firestore Database to see data
-
Check Console Logs
Monitor for any Firebase errors:
Optional: Firebase Emulator Suite¶
For local development without using production Firebase:
- Install Firebase CLI
- Login to Firebase
- Initialize Emulators
- Start Emulators
- Connect App to Emulators
In lib/main.dart, add before initializing Firebase:
if (kDebugMode) {
await FirebaseAuth.instance.useAuthEmulator('localhost', 9099);
FirebaseFirestore.instance.useFirestoreEmulator('localhost', 8080);
}
Troubleshooting¶
Google Sign-In not working on Android
- Verify SHA-1 certificate is added to Firebase Console
- Check package name matches in Firebase and
build.gradle - Ensure
google-services.jsonis in the correct location - Rebuild the app:
flutter clean && flutter run
Firestore permission denied
- Check security rules in Firebase Console
- Ensure user is authenticated
- Verify rules allow the operation
- Check console logs for specific error messages
Firebase initialization failed
- Verify
google-services.jsonis present - Run
flutterfire configureto regenerate configuration - Check
firebase_options.darthas correct project ID - Ensure Firebase dependencies are up to date in
pubspec.yaml
Web app shows 'Firebase: No Firebase App' error
- Verify
firebase_options.dartis generated - Check that
DefaultFirebaseOptions.currentPlatformis used - Ensure Firebase is initialized before app runs
Best Practices¶
Use Multiple Projects
Create separate Firebase projects for development, staging, and production environments.
Monitor Usage
Keep an eye on Firebase usage in the console to avoid exceeding free tier limits.
Backup Firestore
Regularly export Firestore data for backups: bash gcloud firestore export gs://your-bucket/backups
!!! tip "Secure API Keys" - Never commit Firebase config files to public repositories - Add to .gitignore: google-services.json, GoogleService-Info.plist - Use environment variables for sensitive data
Next Steps¶
- Explore Firebase Integration
- Learn About Security Rules
- Deploy to Firebase Hosting
- Monitor with Firebase Analytics
Your Firebase setup is complete!