Complete the code to initialize Firebase in a Next.js project.
import { initializeApp } from 'firebase/app'; const firebaseConfig = { apiKey: [1], authDomain: 'your-project.firebaseapp.com', projectId: 'your-project', }; const app = initializeApp(firebaseConfig); export default app;
You must provide your actual Firebase API key as a string to initialize the app correctly.
Complete the code to import Firestore from Firebase in Next.js.
import { [1] } from 'firebase/firestore'; const db = [1](app); export { db };
The function getFirestore is used to get the Firestore instance from the Firebase app.
Fix the error in the code to add a document to Firestore.
import { collection, [1] } from 'firebase/firestore'; const addUser = async (db, user) => { const usersCol = collection(db, 'users'); await [1](usersCol, user); };
The addDoc function is used to add a new document to a collection in Firestore.
Fill both blanks to correctly fetch documents from Firestore in Next.js.
import { collection, [1] } from 'firebase/firestore'; const fetchUsers = async (db) => { const usersCol = collection(db, 'users'); const snapshot = await [2](usersCol); return snapshot.docs.map(doc => doc.data()); };
The getDocs function is imported and used to fetch all documents from a collection snapshot.
Fill all three blanks to correctly configure Firebase Authentication with Next.js.
import { getAuth, [1], [2] } from 'firebase/auth'; const auth = getAuth(app); const signIn = async (email, password) => { try { const userCredential = await [3](auth, email, password); return userCredential.user; } catch (error) { throw error; } };
Import signInWithEmailAndPassword and createUserWithEmailAndPassword for authentication. Use signInWithEmailAndPassword to sign in users.