
Cloud
Learning Level
Firebase Project Setup is the foundation for building scalable, serverless applications. It involves creating a Firebase project, configuring services, and setting up authentication and databases.
By the end of this lesson, you'll understand:
Firebase is Google's comprehensive platform for building web and mobile applications with:
# Login to Firebase
firebase login
# Create new project (interactive)
firebase init// 1. Go to https://console.firebase.google.com
// 2. Click "Create Project"
// 3. Enter project name: "my-app"
// 4. Enable Google Analytics (optional)
// 5. Create Firebase web app# Install Firebase CLI globally
npm install -g firebase-tools
# Install Firebase SDK in your project
npm install firebase
# Verify installation
firebase --version# Navigate to your project directory
cd my-app
# Initialize Firebase
firebase init
# Select features:
# โ Firestore
# โ Functions
# โ Hosting
# โ Storage
# โ Emulators// firebase-config.js
import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
import { getFirestore } from "firebase/firestore";
import { getStorage } from "firebase/storage";
const firebaseConfig = {
apiKey: "YOUR_API_KEY",
authDomain: "your-app.firebaseapp.com",
projectId: "your-app",
storageBucket: "your-app.appspot.com",
messagingSenderId: "1234567890",
appId: "1:1234567890:web:abcdef123456"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
// Initialize services
export const auth = getAuth(app);
export const db = getFirestore(app);
export const storage = getStorage(app);firebase init firestorefirestore.rules:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null;
}
}
}firebase init storagestorage.rules:
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}// Enable in Firebase Console โ Authentication โ Sign-in method
// - Email/Password
// - Google
// - GitHub
// - Facebook
// - Phone
// Using Email/Password Auth
import { createUserWithEmailAndPassword, signInWithEmailAndPassword } from "firebase/auth";
async function signUp(email, password) {
try {
const userCredential = await createUserWithEmailAndPassword(auth, email, password);
return userCredential.user;
} catch (error) {
console.error("Sign up error:", error);
}
}
async function signIn(email, password) {
try {
const userCredential = await signInWithEmailAndPassword(auth, email, password);
return userCredential.user;
} catch (error) {
console.error("Sign in error:", error);
}
}firebase init functions
# Select language: JavaScript or TypeScript
# Select npm or yarnfunctions/index.js:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.helloWorld = functions.https.onRequest((request, response) => {
response.json({ message: "Hello from Firebase!" });
});
// Firestore trigger
exports.onUserCreate = functions.auth.user().onCreate((user) => {
console.log("New user created:", user.uid);
return admin.firestore().collection('users').doc(user.uid).set({
email: user.email,
createdAt: admin.firestore.FieldValue.serverTimestamp(),
});
});{
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
},
"storage": {
"rules": "storage.rules"
},
"hosting": {
"public": "public",
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
"rewrites": [{
"source": "**",
"destination": "/index.html"
}]
},
"functions": {
"source": "functions"
}
}# Deploy all services
firebase deploy
# Deploy only Firestore rules
firebase deploy --only firestore:rules
# Deploy only Cloud Functions
firebase deploy --only functions
# Deploy only hosting
firebase deploy --only hosting# Initialize emulators
firebase init emulators
# Start emulators
firebase emulators:startimport { connectAuthEmulator, getAuth } from "firebase/auth";
import { connectFirestoreEmulator, getFirestore } from "firebase/firestore";
const auth = getAuth();
const db = getFirestore();
if (process.env.NODE_ENV === 'development') {
connectAuthEmulator(auth, 'http://localhost:9099');
connectFirestoreEmulator(db, 'localhost', 8080);
}// .env.local
REACT_APP_FIREBASE_API_KEY=...
REACT_APP_FIREBASE_AUTH_DOMAIN=...
REACT_APP_FIREBASE_PROJECT_ID=...
// In your app
const firebaseConfig = {
apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
authDomain: process.env.REACT_APP_FIREBASE_AUTH_DOMAIN,
projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
};// admin-sdk.js (Backend only)
import admin from 'firebase-admin';
const serviceAccount = require('./serviceAccountKey.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
projectId: 'your-app'
});
export const adminDb = admin.firestore();
export const adminAuth = admin.auth();.firebaserc:
{
"projects": {
"default": "my-app-prod",
"staging": "my-app-staging",
"development": "my-app-dev"
}
}firebase deploy --project=staging
firebase deploy --project=developmentExplore Firebase Authentication for user management, or Firestore for real-time data storage.
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep โข Build Real โข Verify Skills โข Launch Forward