Backend Setup - chintan992/letsstream2 GitHub Wiki
Backend Setup Guide
This guide covers how to set up the backend infrastructure for Let's Stream V2.0.
Firebase Setup
1. Create Firebase Project
- Go to Firebase Console
- Click "Add Project"
- Enter project name and follow setup wizard
- Enable Google Analytics (recommended)
2. Set Up Authentication
- In Firebase Console, go to Authentication > Sign-in method
- Enable the following providers:
- Email/Password
- Google Sign-in
- Configure OAuth consent screen if required
3. Configure Firestore
- Go to Firestore Database
- Create database in your preferred region
- Start in production mode
- Apply the following security rules:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// Helper functions
function isAuthenticated() {
return request.auth != null;
}
function isOwner(userId) {
return isAuthenticated() && request.auth.uid == userId;
}
// User preferences
match /userPreferences/{userId} {
allow read, write: if isOwner(userId);
}
// Watch history
match /watchHistory/{documentId} {
allow read: if isAuthenticated() && resource.data.user_id == request.auth.uid;
allow create: if isAuthenticated() && request.resource.data.user_id == request.auth.uid;
allow update, delete: if isAuthenticated() && resource.data.user_id == request.auth.uid;
}
// Favorites
match /favorites/{documentId} {
allow read: if isAuthenticated() && resource.data.user_id == request.auth.uid;
allow create: if isAuthenticated() && request.resource.data.user_id == request.auth.uid;
allow update, delete: if isAuthenticated() && resource.data.user_id == request.auth.uid;
}
// Watchlist
match /watchlist/{documentId} {
allow read: if isAuthenticated() && resource.data.user_id == request.auth.uid;
allow create: if isAuthenticated() && request.resource.data.user_id == request.auth.uid;
allow update, delete: if isAuthenticated() && resource.data.user_id == request.auth.uid;
}
// Default deny
match /{document=**} {
allow read, write: if false;
}
}
}
4. Get Configuration Keys
- Go to Project Settings > General
- Scroll to "Your apps" section
- Click web platform (</>)
- Register app and get configuration
- Copy configuration values to your
.envfile:VITE_FIREBASE_API_KEY=your_api_key VITE_FIREBASE_AUTH_DOMAIN=your_auth_domain VITE_FIREBASE_PROJECT_ID=your_project_id VITE_FIREBASE_STORAGE_BUCKET=your_storage_bucket VITE_FIREBASE_MESSAGING_SENDER_ID=your_messaging_sender_id VITE_FIREBASE_APP_ID=your_app_id VITE_FIREBASE_MEASUREMENT_ID=your_measurement_id
Collection Structure
userPreferences Collection
{
user_id: string,
isWatchHistoryEnabled: boolean,
accentColor: string,
created_at: timestamp,
updated_at: timestamp
}
watchHistory Collection
{
user_id: string,
media_id: string,
media_type: 'movie' | 'tv',
title: string,
poster_path: string,
watched_at: timestamp,
progress: number // Playback progress in seconds
}
favorites Collection
{
user_id: string,
media_id: string,
media_type: 'movie' | 'tv',
title: string,
poster_path: string,
added_at: timestamp
}
watchlist Collection
{
user_id: string,
media_id: string,
media_type: 'movie' | 'tv',
title: string,
poster_path: string,
added_at: timestamp
}
Rate Limiting
The project includes built-in rate limiting for API calls. Configure the limits in:
src/utils/firestore-rate-limiter.tssrc/utils/rate-limiter.ts
Error Handling
Implement error tracking:
- Set up Firebase Crashlytics
- Configure error boundaries in React
- Monitor Firestore quota usage
- Set up alerts for authentication issues
Security Best Practices
-
Authentication
- Enable email verification
- Set password requirements
- Configure OAuth properly
-
Firestore
- Use security rules
- Implement data validation
- Set up backups
-
General
- Enable Firebase App Check
- Configure CORS policies
- Set up proper authentication domains