Firebase Initialization
import firebase from 'firebase/app';
import 'firebase/auth';
import 'firebase/firestore';
import 'firebase/storage';
const firebaseConfig = {
apiKey: 'YOUR_API_KEY',
authDomain: 'YOUR_AUTH_DOMAIN',
projectId: 'YOUR_PROJECT_ID',
storageBucket: 'YOUR_STORAGE_BUCKET',
appId: 'YOUR_APP_ID',
};
firebase.initializeApp(firebaseConfig);
const auth = firebase.auth();
const db = firebase.firestore();
const storage = firebase.storage();
Authentication
// Sign up with email and password
auth.createUserWithEmailAndPassword(email, password)
.then((userCredential) => {
// User creation successful
const user = userCredential.user;
})
.catch((error) => {
// An error occurred
const errorCode = error.code;
const errorMessage = error.message;
});
// Sign in with email and password
auth.signInWithEmailAndPassword(email, password)
.then((userCredential) => {
// User authentication successful
const user = userCredential.user;
})
.catch((error) => {
// An error occurred
const errorCode = error.code;
const errorMessage = error.message;
});
// Sign out
auth.signOut()
.then(() => {
// User signed out successfully
})
.catch((error) => {
// An error occurred
});
// Get the currently signed-in user
const currentUser = auth.currentUser;
Firestore (Database)
// Get a reference to a collection
const collectionRef = db.collection('collectionName');
// Add a document to a collection
collectionRef.add({ key: 'value' })
.then((docRef) => {
// Document added successfully
const documentId = docRef.id;
})
.catch((error) => {
// An error occurred
});
// Update a document
collectionRef.doc('documentId').update({ key: 'value' })
.then(() => {
// Document updated successfully
})
.catch((error) => {
// An error occurred
});
// Delete a document
collectionRef.doc('documentId').delete()
.then(() => {
// Document deleted successfully
})
.catch((error) => {
// An error occurred
});
// Get a document
collectionRef.doc('documentId').get()
.then((doc) => {
if (doc.exists) {
// Document data exists
const documentData = doc.data();
} else {
// Document doesn't exist
}
})
.catch((error) => {
// An error occurred
});
// Query documents in a collection
collectionRef.where('field', '==', 'value').get()
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
// Access each document
const documentData = doc.data();
});
})
.catch((error) => {
// An error occurred
});
Storage
// Get a reference to a file in storage
const fileRef = storage.ref().child('path/to/file.jpg');
// Upload a file
fileRef.put(file)
.then((snapshot) => {
// File uploaded successfully
const downloadUrl = snapshot.downloadURL;
})
.catch((error) => {
// An error occurred
});
// Download a file
fileRef.getDownloadURL()
.then((downloadUrl) => {
// File download URL obtained successfully
})
.catch((error) => {
// An error occurred
});
// Delete a file
fileRef.delete()
.then(() => {
// File deleted successfully
})
.catch((error) => {
// An error occurred
});
This cheatsheet provides a basic overview of some common operations in Firebase.
ย