Blog>
Snippets

Initializing Firebase in a React Project

Demonstrates how to integrate and initialize Firebase in a React application to enable real-time data syncing.
// src/firebase.js
import { initializeApp } from 'firebase/app';
import { getFirestore } from 'firebase/firestore';
import { getAuth, GoogleAuthProvider } from 'firebase/auth';

const firebaseConfig = {
    apiKey: 'YOUR_API_KEY',
    authDomain: 'YOUR_AUTH_DOMAIN',
    projectId: 'YOUR_PROJECT_ID',
    storageBucket: 'YOUR_STORAGE_BUCKET',
    messagingSenderId: 'YOUR_MESSAGING_SENDER_ID',
    appId: 'YOUR_APP_ID'
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
// Initialize Cloud Firestore and get a reference to the service
const db = getFirestore(app);
// Initialize Firebase Authentication and get a reference to the service
const auth = getAuth(app);
const provider = new GoogleAuthProvider();

export { db, auth, provider };
This code snippet demonstrates how to initialize Firebase in a React project. It imports the necessary Firebase modules, sets up the Firebase configuration with your project's specific settings, and initializes Firebase App, Firestore, and Authentication services. It also sets up the Google Auth Provider for use with Firebase Authentication. Finally, it exports the initialized services for use in other parts of the application.